설치 환경
spring-boot : 2.7.2
swagger : 3.0.0
build.gradle에 의존성 추가
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
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();
}
}
http://[호스트ip]/swagger-ui/로 접속하면,다음과 같이 접속이 된다.
처음엔 http://[호스트ip]/swagger-ui.html 로 접속했는데, swagger 3.0부터는 접속 url이 바꾸었다고 한다.
설치하면서 다음과 같은 오류가 발생하였다.
다음 블로그 에서 Spring boot 2.6버전 이후에 spring.mvc.pathmatch.matching-strategy 값이 ant_apth_matcher에서 path_pattern_parser로 변경되면서 발생한다고 하여, application.properties에 추가하고 서버를 재시작하였다.
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
하지만, 나는 그래도 문제가 발생하였고, 다음 stackoverflow 글을 참고하여 application class에 @EnableWevMvc를 추가하고 오류를 해결할 수 있었다.
@SpringBootApplication
@EnableWebMvc
public class SpringApplication {
public static void main(String[] args) {
SpringApplication.run(SpringApplication.class, args);
}
}