๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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 ์ฒด์ด๋‹ ๋ฉ”์„œ๋“œ

http://springfox.github.io/springfox/javadoc/2.9.2/springfox/documentation/spring/web/plugins/Docket.html

  • 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 ์ ์šฉ์‹œ

swagger ๊ด€๋ จ ์ฃผ์†Œ๋“ค์„ ๋ฌด์‹œํ•ด์ฃผ๋Š” ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋”๋ณด๊ธฐ
@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

 

๋…ธ์…˜