REST API with Java — Part 3

Swagger

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
@Configuration
public class SwaggerConfig {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/api/**"))
.build()
.useDefaultResponseMessages(false);
}

private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.title("Books API")
.description("Books API demonstration")
.contact(new Contact("Contact Name", "www.domain.com", "contact@email.com"))
.version("1.0")
.build();
}

}
@RestController
@RequestMapping("/api/v1/books")
@AllArgsConstructor
public class BooksController {

@Operation( summary = "Find books", description = "Get registered books" )
@GetMapping
public ResponseEntity<List<BookResponse>> getBooks() {
// implementation omitted
}

@Operation( summary = "Find book by id", description = "Find book by id",
responses = { @ApiResponse( responseCode = "404", description = "Book not found") }
)
@GetMapping("/{id}")
public ResponseEntity<BookResponse> getBook(@PathVariable("id") Integer id) {
// implementation omitted
}

@Operation( summary = "Register new book", description = "Register new book",
responses = { @ApiResponse( responseCode = "400", description = "Invalid Request data" ) }
)
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<BookResponse> createBook(@RequestBody @Valid BookRequest request) {
// implementation omitted
}

@Operation(
summary = "Update book info", description = "Update book info",
responses = {
@ApiResponse( responseCode = "400", description = "Invalid Request data" ),
@ApiResponse( responseCode = "404", description = "Book not found" )
}
)
@PutMapping("{id}")
public ResponseEntity<BookResponse> updateBook(@PathVariable("id") Integer id, @RequestBody @Valid BookRequest request) {
// implementation omitted
}

@Operation(
summary = "Delete book by id", description = "Delete book by id",
responses = { @ApiResponse( responseCode = "404", description = "Book not found") }
)
@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity deleteBook(@PathVariable("id") Integer id) {
// implementation omitted
}

@Operation( summary = "Find reviews", description = "Get review of a book" )
@GetMapping("{bookId}/reviews")
public ResponseEntity<List<ReviewResponse>> getReviews(@PathVariable("bookId") Integer bookId) {
// implementation omitted
}

@Operation(
summary = "Add new review", description = "Add new review to a book",
responses = {
@ApiResponse( responseCode = "400", description = "Invalid Request data" ),
@ApiResponse( responseCode = "404", description = "Book not found" )
}
)
@PostMapping("{bookId}/reviews")
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<ReviewResponse> createReview(@PathVariable("bookId") Integer bookId, @RequestBody @Valid ReviewRequest request) {
// implementation omitted
}
}
Swagger UI page
Operation executed in Swagger UI

--

--

--

Software Craftsman

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Setting up your NAS Box, 2021 Edition, Part 3

cLabs Announces Acquisition of Leading Cross-Chain Architecture Firm Summa, Strengthening the Celo…

Unity Tips: .meta Files

SOLID Principles in Swift

How to Use Seeder in Laravel

Gin 101: Add requestId automatically

Python Multi-Processing vs Multi-Threading

Troubleshooting Unexplained Build Failures: the Mysterious Case of ‘Nothing Has Changed’

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tiago Albuquerque

Tiago Albuquerque

Software Craftsman

More from Medium

The Spring MVC Architecture

REST API with Java — Part 1

Spring Boot Security — Authentication Configuration

SpringBoot and Rest APIs in Java