Spring boot Swagger ์„ค์ • (gradle) ๐Ÿ

bo-yoonยท2021๋…„ 5์›” 9์ผ
2

springboot settings

๋ชฉ๋ก ๋ณด๊ธฐ
2/2

โ— ์Šคํ”„๋ง ๋ถ€ํŠธ swagger๋ฅผ ์„ค์ •ํ•ด๋ณด์ž

1. ์‚ฌ์šฉํ•  gradle dependency ์ฐพ๊ธฐ

gradle ๋””ํŽœ๋˜์‹œ ์ฐธ๊ณ  ์— ๋“ค์–ด๊ฐ€์„œ ์›ํ•˜๋Š” ๋ฒ„์ „์œผ๋กœ ์„ ํƒํ•ด์„œ ๋“ค์–ด๊ฐ„๋‹ค.

๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋“ฏ ํ•œ 2.9.2๋ฅผ ๊ณจ๋ž๋‹ค.



2. ํŽ˜์ด์ง€์— ๋‚˜์˜ค๋Š” gradle ์˜์กด์„ฑ์„ ๋‚ด ํ”„๋กœ์ ํŠธ build.gradle์— ์ถ”๊ฐ€ํ•œ๋‹ค.

์ถ”๊ฐ€๋กœ ์œ„์— ์˜์กด์„ฑ์„ ๋ณต์‚ฌํ•ด swagger2๋„ ์ž„ํฌํŠธ ํ•ด์ค€๋‹ค.

//https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'



3. SwaggerConfig๋ฅผ ๋งŒ๋“ ๋‹ค.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket restAPI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hobbybee"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("hobbybee Spring Boot REST API")
                .version("1.0.0")
                .description("์ทจ๋ฏธ ์ƒํ™œ ์ปค๋ฎค๋‹ˆํ‹ฐ hobbybee์˜ swagger api ์ž…๋‹ˆ๋‹ค.")
                .build();
    }
}

  • ์ฃผ์š” ์˜ต์…˜
    • apis : ๋Œ€์ƒ ํŒจํ‚ค์ง€ ์„ค์ •
    • paths : ์–ด๋–ค ์‹์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” api ๋ฅผ ๋ณด์—ฌ์ค„๊ฒƒ์ธ์ง€?
      any๋Š” ๊ทธ๋ƒฅ ์ „๋ถ€๋‹ค. ๋งŒ์•ฝ, member/ ๋ผ๊ณ  ์„ค์ •ํ•˜๋ฉด member๋กœ ์‹œ์ž‘ํ•˜๋Š” api ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋‹ค.

4. web page ์ ‘์†

http://localhost:8090/swagger-ui.html

5. ์ ์šฉ ํ™•์ธ


@RestController
@RequestMapping(value = "/member/v1")
@Api(tags = {"ํšŒ์›๊ฐ€์ž… API"})
@RequiredArgsConstructor
public class JoinController {

    @PostMapping(value = "/join")
    @ApiOperation(value = "ํšŒ์›๊ฐ€์ž…", response = Join.class)
    public ResponseEntity<T> signUp() {
    
    }
    
 }

profile
๊ฐœ๋ฐœ ๋กœ๊ทธ ๐ŸŽ ๐ŸŽ ๐ŸŽ

0๊ฐœ์˜ ๋Œ“๊ธ€