Swagger UI
springfox-swagger-ui
- Spring 환경에서 Swagger UI를 세팅해주는 라이브러리
- 접속 URL : {{path}}/swagger-ui/index.html
환경
- Spring Boot 2.7.0
- springfox-boot-starter 3.0.0
- springfox:springfox-swagger-ui 3.0.0
사용
dependencies {
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
}
- application.yml
Spring Boot 2.6부터 PATH_PATTERN_PARSER가 기본 값이 되어서 호환이 되지 않음. 아래와 같이 설정 변경이 필요함
spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER
- SwaggerConfiguration.java
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)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfo(
"Title",
"Description",
"1.0",
"Term of service URL",
new Contact("Contact name", "Contact URL", "Contact email"),
"License",
"License URL",
Collections.emptyList()
));
}
}
springdoc을 쓰자
- springfox의 마지막 업데이트는 20년 7월이고 이후 업데이트가 되지 않고 있음
- 위의
PATH_PATTERN_PARSER 호환 문제도 그래서 발생하는게 아닌가 싶음 (PATH_PATTERN_PARSER 가 SpringBoot 기본 설정이 된게 21년 11월로 (boot 2.6), springfox 마지막 업데이트 이후임)
springdoc-openapi-ui
사용
dependencies {
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.9'
}
springdoc.swagger-ui.path: /api-document.html
springdoc.cache.disabled: true
springdoc.override-with-generic-response: false
@RestController
@RequestMapping("/members")
@Tag(name = "/members", description = "사용자")
public class MemberController {
@GetMapping("/{id}")
@Operation(summary = "사용자 조회", description = "사용자 번호로 사용자 조회")
public Object getById(
@Parameter(
name = "번호",
in = ParameterIn.PATH,
description = "사용자 번호",
required = true,
example = "1"
)
@PathVariable("id") Long id
) {}
}