BAEL-3609: Improve article Documenting a Spring REST API Using OpenAPI (#9334)
3.0
This commit is contained in:
@@ -25,6 +25,13 @@ import com.baeldung.springdoc.exception.BookNotFoundException;
|
||||
import com.baeldung.springdoc.model.Book;
|
||||
import com.baeldung.springdoc.repository.BookRepository;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/book")
|
||||
public class BookController {
|
||||
@@ -32,8 +39,15 @@ public class BookController {
|
||||
@Autowired
|
||||
private BookRepository repository;
|
||||
|
||||
// @formatter:off
|
||||
@Operation(summary = "Get a book by its id")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Found the book",
|
||||
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Book.class)) }),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid id supplied", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "Book not found", content = @Content) }) // @formatter:on
|
||||
@GetMapping("/{id}")
|
||||
public Book findById(@PathVariable long id) {
|
||||
public Book findById(@Parameter(description = "id of book to be searched") @PathVariable long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new BookNotFoundException());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.springdoc.kotlin
|
||||
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.Id
|
||||
import javax.validation.constraints.NotBlank
|
||||
import javax.validation.constraints.Size
|
||||
|
||||
@Entity
|
||||
data class Foo(
|
||||
@Id
|
||||
val id: Long = 0,
|
||||
|
||||
@NotBlank
|
||||
@Size(min = 0, max = 50)
|
||||
val name: String = ""
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.springdoc.kotlin
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation
|
||||
import io.swagger.v3.oas.annotations.media.ArraySchema
|
||||
import io.swagger.v3.oas.annotations.media.Content
|
||||
import io.swagger.v3.oas.annotations.media.Schema
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
class FooController() {
|
||||
val fooList: List<Foo> = listOf(Foo(1, "one"), Foo(2, "two"))
|
||||
|
||||
@Operation(summary = "Get all foos")
|
||||
@ApiResponses(value = [
|
||||
ApiResponse(responseCode = "200", description = "Found Foos", content = [
|
||||
(Content(mediaType = "application/json", array = (
|
||||
ArraySchema(schema = Schema(implementation = Foo::class)))))]),
|
||||
ApiResponse(responseCode = "400", description = "Bad request", content = [Content()]),
|
||||
ApiResponse(responseCode = "404", description = "Did not find any Foos", content = [Content()])]
|
||||
)
|
||||
@GetMapping("/foo")
|
||||
fun getAllFoos(): List<Foo> = fooList
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# custom path for swagger-ui
|
||||
springdoc.swagger-ui.path=/swagger-ui-custom.html
|
||||
springdoc.swagger-ui.operationsSorter=method
|
||||
|
||||
# custom path for api docs
|
||||
springdoc.api-docs.path=/api-docs
|
||||
|
||||
Reference in New Issue
Block a user