From e703663759812336ccb2c0c4c4c09800eb6b60f8 Mon Sep 17 00:00:00 2001 From: apeterlic Date: Fri, 19 Aug 2022 12:55:36 +0200 Subject: [PATCH] Add @ApiOperation vs @ApiResponse in Swagger (#12570) * Add @ApiOperation vs @ApiResponse in Swagger * Minor fixes * Fix - rename package --- .../SwaggerDemoApplication.java | 13 +++++++ .../config/SwaggerConfiguration.java | 23 +++++++++++ .../controller/CustomerController.java | 38 +++++++++++++++++++ .../response/CustomerResponse.java | 27 +++++++++++++ .../response/ErrorResponse.java | 23 +++++++++++ .../service/CustomerService.java | 8 ++++ .../service/DefaultCustomerService.java | 13 +++++++ 7 files changed, 145 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/SwaggerDemoApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/config/SwaggerConfiguration.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/controller/CustomerController.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/CustomerResponse.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/ErrorResponse.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/CustomerService.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/DefaultCustomerService.java diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/SwaggerDemoApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/SwaggerDemoApplication.java new file mode 100644 index 0000000000..8dec139917 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/SwaggerDemoApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerresponses; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SwaggerDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerDemoApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/config/SwaggerConfiguration.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/config/SwaggerConfiguration.java new file mode 100644 index 0000000000..5a4d237d97 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/config/SwaggerConfiguration.java @@ -0,0 +1,23 @@ +package com.baeldung.swaggerresponses.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class SwaggerConfiguration { + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + +} + diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/controller/CustomerController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/controller/CustomerController.java new file mode 100644 index 0000000000..0811af56ec --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/controller/CustomerController.java @@ -0,0 +1,38 @@ +package com.baeldung.swaggerresponses.controller; + +import com.baeldung.swaggerresponses.response.CustomerResponse; +import com.baeldung.swaggerresponses.response.ErrorResponse; +import com.baeldung.swaggerresponses.service.CustomerService; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api("Customer Controller") +@RestController +@RequestMapping("/customers") +public class CustomerController { + + private final CustomerService customerService; + + public CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + + @ApiOperation(value = "Gets customer by ID", response = CustomerResponse.class, notes = "Customer must exist") + @ApiResponses(value = { + @ApiResponse(code = 400, message = "Invalid ID supplied"), + @ApiResponse(code = 404, message = "Customer not found"), + @ApiResponse(code = 500, message = "Internal server error", response = ErrorResponse.class) }) + @GetMapping("/{id}") + public ResponseEntity getCustomer(@PathVariable("id") Long id) { + return ResponseEntity.ok(customerService.getById(id)); + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/CustomerResponse.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/CustomerResponse.java new file mode 100644 index 0000000000..9ec96dd4ac --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/CustomerResponse.java @@ -0,0 +1,27 @@ +package com.baeldung.swaggerresponses.response; + +public class CustomerResponse { + + private final Long id; + private final String firstName; + private final String lastName; + + public CustomerResponse(Long id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Long getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/ErrorResponse.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/ErrorResponse.java new file mode 100644 index 0000000000..7779712e6f --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/response/ErrorResponse.java @@ -0,0 +1,23 @@ +package com.baeldung.swaggerresponses.response; + +public class ErrorResponse { + + private String error; + private String message; + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/CustomerService.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/CustomerService.java new file mode 100644 index 0000000000..92ba1037d3 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/CustomerService.java @@ -0,0 +1,8 @@ +package com.baeldung.swaggerresponses.service; + +import com.baeldung.swaggerresponses.response.CustomerResponse; + +public interface CustomerService { + + CustomerResponse getById(Long id); +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/DefaultCustomerService.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/DefaultCustomerService.java new file mode 100644 index 0000000000..ecced541ae --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerresponses/service/DefaultCustomerService.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerresponses.service; + +import com.baeldung.swaggerresponses.response.CustomerResponse; + +import org.springframework.stereotype.Service; + +@Service +public class DefaultCustomerService implements CustomerService { + @Override + public CustomerResponse getById(Long id) { + return new CustomerResponse(1L, "Jane", "Doe"); + } +}