Skip to content

Commit

Permalink
Add springdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
G5andeepD committed Nov 25, 2024
1 parent 495bf34 commit 32aee05
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
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

0 comments on commit 32aee05

Please sign in to comment.