Swagger(스웨거)는 개발자가 REST API 서비스를 설계, 빌드, 문서화할 수 있도록 하는 프로젝트이다.
Swagger는 다음과 같은 경우에 유용하게 사용된다.
최근 Spring에서 밀고 있는건 이름에서도 느낌이 오겠지만 SpringDoc이다.
SpringDoc가 세팅하는 것에 있어서 더 쉬운 것 같다.
/* build.gradle */
dependencies {
// ..
implementation 'io.springfox:springfox-boot-starter:3.0.0'
// ..
}
🚨이런 에러가 뜬다면, 추가해주자.
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
/* application.properties */
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
/* application.yml */
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springswagger.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Practice Swagger")
.description("practice swagger config")
.version("1.0")
.build();
}
}
@RestController
public class HelloController {
@Operation(summary = "test hello", description = "hello api example")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK !!"),
@ApiResponse(responseCode = "400", description = "BAD REQUEST !!"),
@ApiResponse(responseCode = "404", description = "NOT FOUND !!"),
@ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR !!")
})
@GetMapping("/hello")
public ResponseEntity<String> hello(@Parameter(description = "이름", required = true, example = "Park") @RequestParam String name) {
return ResponseEntity.ok("hello " + name);
}
}
/* build.gradle */
dependencies {
// ..
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.5.9'
// ..
}
springdoc:
default-consumes-media-type: application/json
default-produces-media-type: application/json
swagger-ui:
path: /swagger-ui.html
disable-swagger-default-url: true
display-query-params-without-oauth2: true
🍪https://github.com/hocaron/Spring-Study/tree/main/spring-swagger