즐겁게 하하하 2022. 2. 13. 19:46
728x90
- 개발한 REST API 를 편리하게 문서화 해주고 이를 통해서 관리 및 제 3의 사용자가 
  편리하게 API를 호출해보고 테스트 할수 있는 프로젝트
- spring boot 3.6 에서는 아직 호환이 되지 않는다. 2.4.0 권장

- springfox-boot-starter를 gradle dependencies 에 추가함으로써 사용할 수있다.
- 외부에 노출되면 안되는 곳에는 사용주의
- 이전에는 Springfox swagger2 , swaggerUI 를 사용하였다.

@Api 클래스를 스웨거의 리소스로 표시
@ApiOperation 특정 경로의 오퍼레이션 HTTP 메소드 설명
@ApiParam 오퍼레이션 파라미터에 메타 데이터 설명
@ApiResponse 오퍼레이션의 응답 지정
@ApiModelProperty 모델의 속성 데이터 설명
@ApilmplicitParam 메소드 단위의 오퍼레이션 파라미터 설명
@ApilmplicitParams
 
 
 
Controller
________________
@Api(tags = {"API 정보를 제공하는"})
@RestController
@RequestMapping("/api")
public class ApiController {
    //_______________________
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
    //_______________________
    @ApiImplicitParams({
       @ApiImplicitParam(name="x" , value = "x 값" , required = true , dataType = "int" , paramType = "path"),
       @ApiImplicitParam(name="y" , value = "y 값" , required = true , dataType = "int" , paramType = "query")
    })
    @GetMapping("/plus/{x}")
    public int plus( @RequestParam int x , @RequestParam int y){
        return x + y;
    }
    //_______________________
    @ApiResponse(code= 502 , message = "사용자의 나이가 10살 이내일때")
    @ApiOperation(value="사용자의 이름과 나이를 return 하는 메소드")
    @GetMapping("/user")
    public UserRes user( UserReq userReq ){
        return new UserRes(userReq.getName() , userReq.getAge());
    }
    //_______________________
    @PostMapping("/user")
    public UserRes userPost(@RequestBody UserReq userReq){
        return new UserRes(userReq.getName() , userReq.getAge());
    }
    //_______________________
}
 
DTO
_____________________________________
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserReq {

    @ApiModelProperty(value="사용자의 이름" , example = "steve" , required = true)
    private String name;

    @ApiModelProperty(value="사용자의 나이" , example = "10" , required = true)
    private int age;
}
_____________________________________
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRes {

    private String name;
    private int age;
}
 

 

 

728x90