728x90
반응형
API 통신을 하면 보통 데이터 외에 Code나 Message가 같이 오고, 필요한 데이터는 한번 가공해서 받아야 한다.
그런데 이렇게 받게 되는 경우 안의 데이터가 무조건 HashMap으로 받아지게 된다.
이런 경우에 원하는 Object로 받기 위해 제네릭을 사용해 보았지만 역시 맵으로만 받아지게 되어서 구글링을 통해 해결하였다.
다음과 같은 방법으로 사용하면 된다.
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
String response = restTemplate.postForObject(apiUrl, userDto, String.class);
ApiResponseDto<UserDto> apiResponseDto = jsonMapper.readValue(response, new TypeReference<ApiResponseDto<UserDto>>() {});
참고한 스택오버 플로우 : https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class
728x90
반응형
'Java > Spring' 카테고리의 다른 글
JPA QueryDsl 에서 Group By Count 값 가져오기 (0) | 2018.09.28 |
---|---|
RestTemplate에서 PUT으로 파라미터 전송하기 (0) | 2018.09.13 |
[삽질] stomp, sockjs를 이용하여 websocket 연결 시 info 가 404로 나오는 경우 (4) | 2018.03.13 |
이미지 파일 업로드 오류 수정 기록... (0) | 2016.11.08 |
Spring Batch(스프링 배치)에서 작업 시간이 길어지는 경우 (0) | 2016.10.27 |