Error

Spring - Swagger Response 정보가 보이지 않는 문제

Dlise 2024. 7. 30. 15:59

코드 상에서 오류가 발생한 것은 아니지만 내용을 정리하고자 한다.

 

SpringBoot를 활용해 기능을 구현하던 중 API가 제대로 동작하는지 확인하기 위해 Swagger를 활용하였는데 어떤 API 내용은 정상적으로 보이는데 일부 API 내용이 보이지 않았다.

 

아래는 문제 코드와 사진이다.

    @Operation(summary = "사용자 조회", description = "특정 사용자 정보를 조회합니다.")
    @GetMapping("/profile")
    public ResponseEntity<?> getMemberProfile(Authentication authentication) {
        return ResponseEntity.status(HttpStatus.OK).body(memberService.getMemberProfile(authentication.getName()));
    }

 

해결 방법

반환 타입을 ResponseEntity<?>로 정의해 Swagger가 정확한 타입을 추론할 수 없는 것이 문제였다.

    @Operation(summary = "사용자 조회", description = "특정 사용자 정보를 조회합니다.")
    @GetMapping("/profile")
    public ResponseEntity<MemberProfileResponseDto> getMemberProfile(Authentication authentication) {
        return ResponseEntity.status(HttpStatus.OK).body(memberService.getMemberProfile(authentication.getName()));
    }

반환 타입을 특정하니 정상적으로 정보가 보였다.