MSA 학습(5) - Swagger로 API 문서 자동화

엉무개·2021년 8월 27일
0

MSA

목록 보기
6/12

1. build.gradle 추가

//    swagger
    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

2. config 패키지에 SwaggerConfig 작성

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("USER-SERVICE")
                .description("USER API")
                .version("1.0")
                .build();
    }
    @Bean
    public Docket commonApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/user-service/**"))
                .build();
    }
}

3. 컨트롤러에 ApiOperation 어노테이션 추가

//    회원가입
    @ApiOperation(value="회원가입")
    @PostMapping("/user")
    public Long createUser(@RequestBody RequestUserDto dto){
        return userService.createUser(dto);
    }
//    정보수정
    @ApiOperation(value="회원정보 수정")
    @PutMapping("/user")
    public ResponseEntity<ResponseUserDto> modify(@RequestBody RequestUserDto dto){
        return userService.modify(dto);
    }
//    로그인
    @ApiOperation(value="로그인" , notes = "[응답코드]\n로그인 성공 : 200\n로그인 실패 : 404\n 로그인 성공시 token 발급")
    @PostMapping("/login")
    public ResponseEntity<JWTDto> login(@RequestBody LoginDto loginDto){
        return userService.login(loginDto.getEmail(),loginDto.getPassword());
    }

4. 필드에 @ApiModelProperty로 예제 설명


    @ApiModelProperty(example = "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOj22MzA0NDAxMjcsImV4cCI6MTYzMjAzMjEyNywic3ViIjoidGVzdDFAbmF2ZXIuY29tIn0.R-BAIAPMpSdcspxXrnnck6AM8LvNBeIxC5Y4TyG_rKc")
    private String token;

5. http://아이피:포트/swagger-ui.html 접속하여 API 문서 확인

profile
엉덩이가 무거운 개발자

0개의 댓글