springboot에 swagger2를 적용하는 예제를 하나 만들었다.
https://github.com/knight76/springboot2-swagger2
* 버전
SpringBoot 2.1
Swagger2 2.92
Gradle
Swagger Config 파일은 다음과 같다.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("API")
.select()
.apis(and(RequestHandlerSelectors.basePackage("com.example.controller")))
//.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiEndPointsInfo());
}
public ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Spring Boot REST API with Swagger2")
.description("Language Management REST API")
.contact(new Contact("knight76", "http://knight76.tistory.com", "knight76@gmail.com"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("0.0.1-SNAPSHOT")
.build();
}
}
RestController의 (Get|Post|..)Mapping 앞에 붙이는 ApiOperation, ApiResponse를 참고한다.
@ApiOperation(value = "View available cities", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved list"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found")
})
@GetMapping(value = "/cities")
public List<City> getCities() {
List<City> cities = cityService.findAll();
return cities;
}
중요한 부분은 warning 안보기 위한 gradle 설정이다.
ext { swagger2Version = '2.9.2' oldSwagger2Version = '1.5.21' } ..
// not showing WARN i.s.m.p.AbstractSerializableParameter : Illegal DefaultValue -1 for parameter type integer // java.lang.NumberFormatException: For input string: "" implementation("io.springfox:springfox-swagger2:${swagger2Version}") { exclude group: 'io.swagger',module: 'swagger-annotations' exclude group: 'io.swagger',module: 'swagger-models' } implementation "io.swagger:swagger-annotations:${oldSwagger2Version}" implementation "io.swagger:swagger-models:${oldSwagger2Version}"