‼️ 이슈

개인 프로젝트 진행 중 코드를 열심히 수정하고 스웨거에 접속하려고 하니 이런 에러가 났다...
👀 원인 파악
ExceptionHandler를 통해 return 받은 결과는 다음과 같다. 보통 의존성 버전 문제로 발생한다고 한다.
{
"httpStatus": "INTERNAL_SERVER_ERROR",
"message": "Handler dispatch failed: java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)'"
}
흠... 처음에는 단순 스웨거 문제인 줄 알고 열심히 구글링 하다가 이런 내용을 봤다.
@ControllerAdvice 애노테이션과 Swagger 사이에 일어난 충돌이다.
@ControllerAdvice가 없다면 Swagger는 controller class를 가져와 API 정의를 분석한다.
@ControllerAdvice가 있다면 Swagger 응답이 내가 응답을 통일해 둔 방식으로 변환되고, Swagger 웹 페이지는 응답 구조를 인식할 수 없게 된다.
프로젝트의 패키지에서 @ControllerAdvice가 적용될 곳을 명시하면 Swagger 응답이 변환되지 않아 충돌을 해결할 수 있다.
수정사항 중 하나가 예외 처리 한다고 @RestControllerAdvice를 추가한 게 있어서 호다닥 다시 수정을 해줬는데도 똑같은 현상 발생... 🥺
// 기존
@RestControllerAdvice
// 수정
@RestControllerAdvice(annotations = RestController.class)
✅ 해결방법
그러다 스택오버플로우에서 다음과 같은 설정을 하고 해결됐다는 글을 보았다.
springdoc:
override-with-generic-response: false
그랬더니 문제 해결!! 🥹
저게 무슨 설정인가 하고 공식 문서를 찾아보았다.
Springdoc-openapi Properties
springdoc-openapi core properties Parameter name Default Value Description springdoc.api-docs.path /v3/api-docs String, For custom path of the OpenAPI documentation in Json format. springdoc.api-docs.enabled true Boolean. To disable the springdoc-openapi e
springdoc.org
springdoc.override-with-generic-response (default : true)
설명 : Boolean. When true, automatically adds @ControllerAdvice responses to all the generated responses.
설정값이 true면 생성된 모든 응답에 @ControllerAdvice 응답을 자동으로 추가한다고 한다.
나는 false로 설정해 줬기 때문에 springdoc에서 생성된 일반적인 응답을 비활성화하여 문제가 해결됐던 것이다.
👍 참고자료
https://dev-meung.tistory.com/entry/해커톤-HY-THON-트러블슈팅-Swagger-500-에러-Failed-to-load-API-definition
https://stackoverflow.com/questions/79274106/how-to-use-both-restcontrolleradvice-and-swagger-ui-in-spring-boot
'Project > 중고타운' 카테고리의 다른 글
| 프로젝트 회고 (0) | 2025.02.26 |
|---|---|
| 컨트롤러 계층 단위테스트 중 No qualifying bean of type 'JwtProvider' available (0) | 2025.02.19 |
| 프로젝트 개요 (0) | 2025.02.09 |