반응형
개발 전 테스트는 Spring Boot 2.3.4 기준으로 진행했는데
시작하니까 버전이 2.1.X로 바꼈다...
URL도 바뀌고 @EnableSwagger2 넣고, 리소스도 등록해줘야 한다.
1. SpringBoot 2.1.X 기준
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger Configuration 클래스
/**
* https://localhost:8080/v2/api-docs
* https://localhost:8080/swagger-resources/configuration/ui
* https://localhost:8080/swagger-resources/configuration/security
* https://localhost:8080/swagger-ui.html
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.enable(true);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 타이틀")
.description("API 상세소개 및 사용법 등")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
2. SpringBoot 2.3.4 기준
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Swagger Configuration 클래스
/**
* http://localhost:8080/v2/api-docs
* http://localhost:8080/swagger-resources/configuration/ui
* http://localhost:8080/swagger-resources/configuration/security
* http://localhost:8080/swagger-ui/index.html
*/
@Profile({"local", "dev"})
@Configuration
public class SpringFoxConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.enable(true);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 타이틀")
.description("API 상세소개 및 사용법 등")
.version("1.0")
.build();
}
}
추가로 Spring Security를 쓰는 경우 설정
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
... 생략 ...
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**");
}
}
참고
반응형
'백엔드 > SpringBoot' 카테고리의 다른 글
Thymeleaf 템플릿 적용하기 (0) | 2020.12.20 |
---|---|
프로퍼티 클래스 만들기 (0) | 2020.12.19 |
SpringBoot의 Auto Configuration과 Application.properties 설정 (0) | 2020.12.19 |
DTO 따로 만들기(Param, VO) (0) | 2020.08.02 |
Rest Controller 매개변수 어노테이션 (0) | 2020.08.02 |
댓글