study
[spring boot] swagger ์ ์ฉ
gradle
๋๋ณด๊ธฐ
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
SwaggerConfig
๋๋ณด๊ธฐ
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean // Docket : Swagger ์ค์ ์ ํต์ฌ์ด ๋๋ Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.pmc.market"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"PMC Second Project",
"Market - Consumer Rest API",
"1.0.0",
null, null, null, null,
new ArrayList<>()
);
}
}
Docket ์ฒด์ด๋ ๋ฉ์๋
- useDefaultResponseMessage()
- false๋ก ์ค์ ํ๋ฉด swagger์์ ์ ๊ณตํด์ฃผ๋ ์๋ต์ฝ๋(200, 401, 403, 404)์ ๋ํ ๊ธฐ๋ณธ ๋ฉ์ธ์ง๋ฅผ ์ ๊ฑฐํ๋ค.
- ๋ถํ์ํ ์๋ต์ฝ๋์ ๋ฉ์ธ์ง๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํจ
- groupName()
- Docket Bean์ด 1๊ฐ์ด๋ฉด ์๋ต๊ฐ๋ฅ
- Docket Bean์ด 2๊ฐ ์ด์์ผ ๊ฒฝ์ฐ
- groupName์ด ์ถฉ๋ํ์ง ์์์ผ ํ๋ฏ๋ก ๊ฐ Docket Bean์ ๋ฒ์ ผ์ ๋ช ์ํ๋ ๋ฑ ์ด๋ฆ์ ์ค์ ํด ์ค๋ค.
- select()
- ApiSelectorBuilder๋ฅผ ์์ฑํ๋ค.
- apis()
- api ์คํ์ด ์์ฑ๋์ด ์๋ ํจํค์ง๋ฅผ ์ง์
- ์ปจํธ๋กค๋ฌ๊ฐ ์กด์ฌํ๋ ํจํค์ง๋ฅผ basepackage๋ก ์ง์ ํ๋ฉฐ RequestMapping์ด ์ ์ธ๋ API๋ฅผ ๋ฌธ์ํ ํ๋ค.
- paths()
- apis()๋ก ์ ํ๋์ด์ง API์ค ํน์ path ์กฐ๊ฑด์ ๋ง๋ API๋ค์ ๋ค์ ํํฐ๋งํด ๋ฌธ์ํ ํ๋ค.
- apiInfo()
- ์ ๋ชฉ, ์ค๋ช ๋ฑ ๋ฌธ์์ ๋ํ ์ ๋ณด๋ค์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ํธ์ถํ๋ค.
Controller์์ ์ฌ์ฉ ๋ฐฉ๋ฒ
๋๋ณด๊ธฐ
@Api(value = "MainController", tags = "๋ฉ์ธ ์ปจํธ๋กค๋ฌ")
@RestController
@RequestMapping("/common")
public class MainController {
// swagger ์์
@ApiOperation("๋ฉ์ธ ")
@GetMapping("/{id}")
public String mainTest(@ApiParam(value="id ๊ฐ์ ๊ฒ") @PathVariable(value = "id")long id){
String result = "hi"+String.valueOf(id);
return result;
}
}
@Api
- ํด๋น ํด๋์ค๊ฐ Swagger ๋ฆฌ์์ค๋ผ๋ ๊ฒ์ ๋ช ์ํ๋ค.
- value : ํ๊ทธ ๊ฐ
- tags : ์ฌ๋ฌ๊ฐ์ ํ๊ทธ๋ฅผ ๋ช ์ ํ ๋
@ApiOperation
- ํ ๊ฐ์ operation(=API URL๊ณผ method)๋ฅผ ์ ์ธํ๋ค
- value : API์ ๋ํ ๊ฐ๋ตํ ์ค๋ช (์ ๋ชฉ) ์์ฑ
- notes : ๋ ์์ธํ ์ค๋ช
@ApiResponse
- operation์ ๊ฐ๋ฅํ response๋ฅผ ๋ช ์
- code : ์๋ต์ฝ๋ ์์ฑ
- message : ์๋ต์ ๋ํ ์ค๋ช ์์ฑ
- responseHeaders : ํค๋ ์ถ๊ฐ ๊ฐ๋ฅ
@ApiParam
- ํ๋ผ๋ฏธํฐ์ ๋ํ ์ ๋ณด ๋ช ์
- value : ํ๋ผ๋ฏธํฐ ์ ๋ณด ์์ฑ
- required : ํ์๋ฉด true, ์๋๋ฉด false
- example : ํ ์คํธ ํ ๋ ๋ณด์ฌ์ค ์์ ์์ฑ
response๋ฅผ ๊ธ๋ก๋ฒ ๋ฉ์์ง๋ก ์ค์ ํ๋ ๋ฐฉ๋ฒ
swaggerConfig ํ์ผ์์ globalResponseMessage๋ฅผ ์ถ๊ฐ ํด์ค๋ค.
๋๋ณด๊ธฐ
@Bean
public Docket docket() {
List<ResponseMessage> responseMessages = new ArrayList<>();
responseMessages.add(new ResponseMessageBuilder()
.code(200)
.message("OK ~~")
.build());
responseMessages.add(new ResponseMessageBuilder()
.code(404)
.message("Not Found ~~")
.build());
responseMessages.add(new ResponseMessageBuilder()
.code(500)
.message("Internal Server Error ~~")
.build());
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.pmc.market"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.globalResponseMessage(RequestMethod.GET, responseMessages);
}
์ฐธ๊ณ https://victorydntmd.tistory.com/341
Security ์ ์ฉ์
๋๋ณด๊ธฐ
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().headers().frameOptions().disable()
.and()
.authorizeRequests()
.antMatchers("/", "/css/**", "/images/**", "/js/**", "/shops/**").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.logoutSuccessUrl("/");
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/**", "/configuration/ui", "/swagger-resources/**",
"/configuration/security", "/swagger-ui.html/**", "/webjars/**", "/swagger**");
}
}
์ฐธ๊ณ
https://ahea.wordpress.com/2017/01/18/spring-boot-swagger-์ ์ฉ๊ธฐ/
https://jamong-icetea.tistory.com/373
๋ ธ์
'study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Kotlin youtube ์์ฝ์ธ๋ฏํ ๊ฐ์๋ฌธ (0) | 2022.07.16 |
---|---|
@Transactional(readOnly=true) (0) | 2022.03.16 |
[Java] ์์ดํ 5 ์์์ ์ง์ ๋ช ์ํ์ง ๋ง๊ณ ์์กด ๊ฐ์ฒด ์ฃผ์ ์ ์ฌ์ฉํ๋ผ (0) | 2021.05.09 |
[spring boot] mock test (2) | 2021.04.18 |