언어

Swagger annotation 어노테이션 정리

개발자국S2 2023. 8. 30. 13:36

@Operation : API의 응답결과를 기록하기 위함

@Operation(summary= "API 간단 요약")

@Operation(description="API에 대한 상세요구사항 등") 

  ex) @Operation(summary= "Gets customer by ID", description= "Customer must exist")

 

@ApiResponse : 해당 API로 받을 수 있는 응답값 예시

@ApiResponses(value = { @ApiResponse(responseCode = 404, description = "Customer not found")})

@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Ok", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = CustomerResponse.class)) })

*ApiResponses는 @nestjs/swagger에서 제공하지 않는다고 하네..?

 

 


 

스웨거에 기본 파라미터 정의하기 위해 Controller에서 엄청 고군분투했었다. 

@ApiProperty({example:값})로 정의했던 것 같은데 계속 에러가 나서 gpt에 물어보니 @ApiBody()를 사용하라고 했다. 

@ApiBody({example : 값}) 이렇게 정의하라고 했는데, 백틱사용으로 스트링값만 들어가고 막 헤매던와중 해당 파라미터 정의는 dto에서 하는 것임을 깨달았다. 

 

아래 처럼 정의하면 된다. (정렬이 깨지네..)

export class Input{
	@ApiProperty({example : '김길동', type:'string'})
    name: string
}

 

 

 

출처 : https://www.baeldung.com/swagger-operation-vs-apiresponse

 

반응형