Created
April 25, 2022 10:31
-
-
Save senoritadeveloper01/400a6d361881482e1904de63c4bd6083 to your computer and use it in GitHub Desktop.
OpenAPI Documentation Annotations in Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.senoritadev.bookstore.rest; | |
import com.senoritadev.bookstore.model.BookInfo; | |
import com.senoritadev.bookstore.service.BookService; | |
import io.swagger.v3.oas.annotations.Operation; | |
import io.swagger.v3.oas.annotations.Parameter; | |
import io.swagger.v3.oas.annotations.enums.ParameterIn; | |
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 io.swagger.v3.oas.annotations.tags.Tag; | |
import lombok.RequiredArgsConstructor; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.validation.annotation.Validated; | |
import org.springframework.web.bind.annotation.*; | |
import javax.validation.Valid; | |
import javax.validation.constraints.Min; | |
import javax.validation.constraints.NotBlank; | |
import javax.validation.constraints.NotNull; | |
@Tag(name = "Books", description = "Endpoints for book operations") | |
@RequiredArgsConstructor | |
@RestController | |
@RequestMapping("/books") | |
@Validated | |
public class BookRestController { | |
private final BookService bookService; | |
@Operation(summary = "Get book info", description = "Get book info by id", tags = { "Books" }) | |
@Parameter(in= ParameterIn.HEADER, description = "Authorization token", name = "X-Auth-Token", content = @Content(schema = @Schema(type = "string"))) | |
@ApiResponses(value = { | |
@ApiResponse(responseCode = "200", description = "Successful operation", | |
content = @Content(schema = @Schema(implementation = BookInfo.class))), | |
@ApiResponse(responseCode = "400", description = "Book with given id does not exist.", content = @Content) | |
}) | |
@GetMapping(value = "/{id}", produces = { "application/json" }) | |
public ResponseEntity<BookInfo> getBookById(@PathVariable(required = true) @NotBlank String id) { | |
return ResponseEntity.ok().body(bookService.getById(id)); | |
} | |
@Operation(summary = "Insert book", description = "Insert a new book", tags = { "Books" }) | |
@Parameter(in= ParameterIn.HEADER, description = "Authorization token", name = "X-Auth-Token", content = @Content(schema = @Schema(type = "string"))) | |
@ApiResponses(value = { | |
@ApiResponse(responseCode = "200", description = "Successful operation", | |
content = @Content(schema = @Schema(implementation = BookInfo.class))) | |
}) | |
@PostMapping(consumes = { "application/json" }) | |
public ResponseEntity<BookInfo> addBook(@Valid @RequestBody BookInfo bookInfo) { | |
return ResponseEntity.ok().body(bookService.add(bookInfo)); | |
} | |
@Operation(summary = "Update book stock", description = "Update book stock by id", tags = { "Books" }) | |
@Parameter(in= ParameterIn.HEADER, description = "Authorization token", name = "X-Auth-Token", content = @Content(schema = @Schema(type = "string"))) | |
@ApiResponses(value = { | |
@ApiResponse(responseCode = "200", description = "Successful operation", | |
content = @Content(schema = @Schema(implementation = BookInfo.class))), | |
@ApiResponse(responseCode = "400", description = "Book with given id does not exist.", content = @Content) | |
}) | |
@PatchMapping(value="/{id}", consumes = "application/json") | |
public ResponseEntity<BookInfo> updateStockById(@PathVariable(required = true) @NotBlank String id, | |
@Valid @RequestBody @NotNull @Min(value = 0) Integer quantity) { | |
return ResponseEntity.ok().body(bookService.updateStockById(id, quantity)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment