Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add springdoc #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions spring-boot-reading-list-rest-api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

37 changes: 24 additions & 13 deletions spring-boot-reading-list-rest-api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -20,23 +20,34 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.25</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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<Book> 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);
Expand Down