์ด๊ธ์ ๋ชฉ์ ์ ํ๋ก์ ํธ API ๊ฐ๋ฐ์ ์ด๋์ ๋ ๋ง์น๊ณ Swagger๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํ๋ ๊ฒ์ด๋ค.
์ ์ฉํ๊ธฐ์ ์์ ์ด ํ๋ก์ ํธ๋ Spring Boot 2.6.2์ Spring Security๊ฐ ์ ์ฉ๋์๋ค.
1. build.gradle์ ์์กด์ฑ ์ถ๊ฐ
//for Swagger
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
2. SwaggerConfig ํด๋์ค ์์ฑ
config ํจํค์ง ๋ด์ SwaggerConfig ํด๋์ค๋ฅผ ์์ฑํด์ค ๋ค ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ค.
- api() ๋ฉ์๋์ apis()์๋ ํด๋น ํ๋ก์ ํธ์ ํจํค์ง ๊ฒฝ๋ก๋ฅผ ์ ๋ ฅํด์ฃผ์ด์ผ ํ๋ค.
- apiInfoMetaData() ๋ฉ์๋์์๋ Swagger ui์์ ๋ณด์ฌ์ง ํ์ดํ, ์ค๋ช ์ ๋ณด๋ฅผ ์ค์ ํ ์ ์๋ค.
package com.teamk.laube.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.teamk.laube.controller"))
.paths(PathSelectors.ant("/*/**"))
.build().apiInfo(apiInfoMetaData());
}
private ApiInfo apiInfoMetaData() {
return new ApiInfoBuilder().title("NAME OF SERVICE")
.description("API Endpoint Decoration")
.contact(new Contact("Dev-Team", "https://www.dev-team.com/", "dev-team@gmail.com"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
๐ต๐ซ ๋ฐ์ํ๋ ์๋ฌ
๋จผ์ ์ฒ์์ ์ฝ๋ ์์ฑํ์ ๋ ๋ฐ์ํ ์๋ฌ๋ EnableSwagger2 ์ด๋ ธํ ์ด์ ์์ ๋ฐ์ํ ์๋ฌ์๋ค.
๊ณ์ํด์ NullPointerException์ด ๋ฐ์ํ์๊ณ , EnableSwagger2 ๋์ EnableWebMVC ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํด๋ดค์ง๋ง ํด๊ฒฐ๋์ง ์์๊ณ , WebMvcConfigurationSupport ํด๋์ค๋ฅผ ์์๋ฐ์ ํ addResourceHandlers ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ๋๋ Exception์ด ํด๊ฒฐํ์๋ค!
3. Spring Security๋ฅผ ์ฌ์ฉ์ค์ด๋ผ๋ฉด ๊ฒฝ๋ก ๊ถํ ๋ถ์ฌํ๊ธฐ
SecurityConfig ํด๋์ค์ ๋ค์ด๊ฐ ๊ถํ ํ์ฉ์ ํด์ค๋ค.
.antMatchers(
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**",)
.permitAll()
๊ตฌ๊ธ๋ง์ ํ๋ค๋ณด๋ swagger-ui.html์ผ๋ก ์ ์ํด์ผ ํ ์ง swagger-ui/index.html์ผ๋ก ์ ์ํด์ผ ํ ์ง ๋ฒ์ ์ ๋ฐ๋ผ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ swagger-ui 404 ์๋ฌ๊ฐ ์ข ์ข ๋ฐ์ํ๋ค.
์์ ์ ์ผ๋ก 2.9.2๋ฒ์ ์ ์ฌ์ฉํ๋ค๋ฉด localhost:8080/swagger-ui.html ๋ก ์ ๊ทผํ๋ฉด ์๋ฌ์์ด swagger ํ์ด์ง๋ฅผ ๋์ธ ์ ์๋ค.

์ถ์ฒ
'Spring Boot > ๊ฐ๋ฐ ๊ธฐ๋ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Spring Cacheable์ ๋์ ์๋ฆฌ (0) | 2024.09.22 |
|---|---|
| Spring Boot ๋ฌด์ค๋จ ๋ฐฐํฌํ๊ธฐ (AWS ec2 + Nginx + Github self -hosted runner) (0) | 2024.03.11 |
| [Spring Security] JWT Tutorial (5) ํ์๊ฐ์ , ๊ถํ ๊ฒ์ฆ (0) | 2022.04.28 |
| [Spring Security] JWT Tutorial (4) Repository ์์ฑ, ๋ก๊ทธ์ธ (0) | 2022.04.28 |
| [Spring Security] JWT Tutorial (3) JWT ์ฝ๋, Security ์ค์ ์ถ๊ฐ (0) | 2022.04.28 |
๋๊ธ