From 32aee0542341600fa2c65b1114fa7ef0e8db0f0d Mon Sep 17 00:00:00 2001 From: G5andeepD Date: Fri, 25 Oct 2024 17:43:33 +0530 Subject: [PATCH 1/2] Add springdoc --- spring-boot-reading-list-rest-api/pom.xml | 37 +++++++++------ .../springboot/controller/BookController.java | 46 +++++++++++++++++++ 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/spring-boot-reading-list-rest-api/pom.xml b/spring-boot-reading-list-rest-api/pom.xml index f9fc9f9c..aca8c125 100644 --- a/spring-boot-reading-list-rest-api/pom.xml +++ b/spring-boot-reading-list-rest-api/pom.xml @@ -1,6 +1,6 @@ - + 4.0.0 org.springframework.boot @@ -20,23 +20,34 @@ org.springframework.boot spring-boot-starter-web - - - org.yaml - snakeyaml - - + + + org.yaml + snakeyaml + + + + + org.yaml + snakeyaml + 2.0 - - org.yaml - snakeyaml - 2.0 - org.springframework.boot spring-boot-starter-test test + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.6.0 + + + io.swagger.core.v3 + swagger-annotations + 2.2.25 + + diff --git a/spring-boot-reading-list-rest-api/src/main/java/com/wso2/choreo/sample/springboot/controller/BookController.java b/spring-boot-reading-list-rest-api/src/main/java/com/wso2/choreo/sample/springboot/controller/BookController.java index 5edfa563..6acb6498 100644 --- a/spring-boot-reading-list-rest-api/src/main/java/com/wso2/choreo/sample/springboot/controller/BookController.java +++ b/spring-boot-reading-list-rest-api/src/main/java/com/wso2/choreo/sample/springboot/controller/BookController.java @@ -2,6 +2,13 @@ import com.wso2.choreo.sample.springboot.model.Book; import com.wso2.choreo.sample.springboot.service.BookService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -14,26 +21,65 @@ public class BookController { @Autowired private BookService service; + @Operation(summary = "Add a new book") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful response", + content = @Content(mediaType = "application/json")) + }) + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "Add a new book", + required = true, + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = Book.class), + examples = @ExampleObject(value = "{ \"id\": 3, \"title\": \"To Kill a Mockingbird\", \"author\": \"Harper Lee\", \"status\": \"READ\" }")) + ) @PostMapping public Book addBook(@RequestBody Book book) { return service.saveBook(book); } + @Operation(summary = "Get all books") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful response", + content = @Content(mediaType = "application/json")) + }) @GetMapping public Collection findAllBooks() { return service.getBooks().values(); } + @Operation(summary = "Get book by ID") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful response", + content = @Content(mediaType = "application/json")) + }) @GetMapping("/id") public Book findBookById(@RequestParam("id") int bookId) { return service.getBookById(bookId); } + @Operation(summary = "Update an existing book's details") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful response", + content = @Content(mediaType = "application/json")) + }) + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "Update an existing book's details", + required = true, + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = Book.class), + examples = @ExampleObject(value = "{ \"id\": 1, \"title\": \"Gulliver's Travels\", \"author\": \"Jonathan Swift\", \"status\": \"READ\" }")) + ) @PutMapping public Book updateBook(@RequestBody Book book) { return service.updateBook(book); } + @Operation(summary = "Delete book by ID") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful response", + content = @Content(mediaType = "application/json")) + }) @DeleteMapping public String deleteBook(@RequestParam("id") int bookId) { return service.deleteBook(bookId); From 35e3bb9c568eedc72fb67ad99d6e0bd099be1a71 Mon Sep 17 00:00:00 2001 From: G5andeepD Date: Wed, 27 Nov 2024 14:11:56 +0530 Subject: [PATCH 2/2] edit oas --- spring-boot-reading-list-rest-api/openapi.yaml | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/spring-boot-reading-list-rest-api/openapi.yaml b/spring-boot-reading-list-rest-api/openapi.yaml index 60a89357..d3254d16 100644 --- a/spring-boot-reading-list-rest-api/openapi.yaml +++ b/spring-boot-reading-list-rest-api/openapi.yaml @@ -67,18 +67,4 @@ paths: description: Successful response content: application/json: {} - /books/id: - get: - summary: Get book by ID - parameters: - - name: id - in: query - description: ID of the book to find - required: true - schema: - type: integer - responses: - "200": - description: Successful response - content: - application/json: {} +