본문 바로가기
백엔드/SpringBoot

Spring Boot에 Swagger 적용하기

by 1005ptr 2020. 12. 10.
반응형

개발 전 테스트는 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/**");
	}
}

참고

https://stackoverflow.com/questions/37671125/how-to-configure-spring-security-to-allow-swagger-url-to-be-accessed-without-aut

 

How to configure Spring Security to allow Swagger URL to be accessed without authentication

My project has Spring Security. Main issue: Not able to access swagger URL at http://localhost:8080/api/v2/api-docs. It says Missing or invalid Authorization header. Screenshot of the browser win...

stackoverflow.com

https://stackoverflow.com/questions/49155420/springfox-swagger-ui-html-unable-to-infer-base-url-caused-by-missing-cookies

 

Springfox swagger-ui.html unable to infer base URL - Caused by missing cookies

We have our Spring Boot services behind an API Gateway. With an earlier version of Springfox - 2.1.2 we had no issues in loading the swagger-ui.html page. This worked with Spring Boot 1.4.3.RELEASE...

stackoverflow.com

 

반응형

댓글