Add @ApiOperation vs @ApiResponse in Swagger (#12570)

* Add @ApiOperation vs @ApiResponse in Swagger

* Minor fixes

* Fix - rename package
This commit is contained in:
apeterlic 2022-08-19 12:55:36 +02:00 committed by GitHub
parent 520c7d7083
commit e703663759
7 changed files with 145 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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<CustomerResponse> getCustomer(@PathVariable("id") Long id) {
return ResponseEntity.ok(customerService.getById(id));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.swaggerresponses.service;
import com.baeldung.swaggerresponses.response.CustomerResponse;
public interface CustomerService {
CustomerResponse getById(Long id);
}

View File

@ -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");
}
}