-
RestTemplate는 응답 객체를 LinkedHashMap으로 파싱한다프레임워크/Spring Boot 2024. 5. 2. 15:00
RestTemplate으로 User서비스에서 데이터를 받아오는 과정에서 발생한 이슈를 정리합니다.
몇 줄 안되는 간단한 코드에서 생각보다 많은 케이스를 만나고 있습니다 ㅎㅎ
분명히 응답이 잘 오는 것을 result 를 log로 찍어서 확인했는데. catch 문이 실행됩니다?
저의 응답 객체의 모양은 이렇습니다.
성공시 result에 데이터가 담기고, 실패시 에러에 데이터가 담겨서 전달됩니다.
그런데 getResult() 를 하면 java.lang.ClassCastException 예외가 발생합니다.
예외를 살펴보면 LinkedHashMap을 AuthUserDetails로 캐스트 할 수 없다고 합니다.
첫 depth의 CustomResponse 는 잘 파싱이 되었는데, 두번째 depth는 주어진 타입이아니고 LinkedHashMap의 형태로 파싱을 하는 모양입니다. 두번째 depth를 convertValue 메소드를 사용해서 다시 한 번 매핑해줍니다.
콘솔을 통해서 변환 전 후가 달라지는 것을 확인할 수 있습니다.
변환 후에는 로그에 AuthUserDetails가 찍히는 것을 볼 수 있습니다.
첫 depth는 잘 되는 것이 맞는지 검증을 해보니, 첫 depth 역시 LinkedHashMap으로 파싱해서 주는 것 같습니다.
일부 코드를 지우고 첫 depth 객체를 리턴하니 동일한 예외가 발생합니다.
Map 형태로 받아서 사용하는 것이라면 크게 문제가 없겠지만 원하는 타입(클래스)로 변경해서 사용해야한 다면 매핑과정이 한번 더 추가 되어야할 것 같습니다~
감사합니다.
'프레임워크 > Spring Boot' 카테고리의 다른 글
Spring Security 다이어그램으로 flow 정리하기 (0) 2024.05.07 SecurityFilterChain을 여러개 쓰고자 한다면 securityMatcher! (0) 2024.05.03 RestTemplate은 4xx를 예외로 처리한다. (0) 2024.05.02 SpringBootApplication이 필요한 bean을 찾지 못하는 경우 (0) 2024.04.25 N + 1 정복하기 <5. Entity Graph> (0) 2024.02.21