on
restTemplate entity to object
restTemplate entity to object
다른 api 를 찔러서 가져온 데이터를 비교해야 할 일이 생겼다.
RestTemplate restTemplate = new RestTemplate(); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://abc.co.kr") .queryParam("projectIdx",150685) .queryParam("workType","SECURITY"); //restTemplate으로 찌를때, 인증 정보가 필요 HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.AUTHORIZATION, masterToken); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity httpEntity = new HttpEntity(headers); ResponseEntity> res = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, httpEntity, new ParameterizedTypeReference>() {}); log.info("데이터 : {}", res.getBody());
1. 가장 먼저 restTemplate 객체 생성
2. 헤더 설정 - 이때 인증 정보가 필요하다면 세팅해줌
3. restTemplate.exchange로 api 쏨
ResponseEntity 으로 받아서 확인했을 때는 데이터 잘 넘어 왔는데, 내가 설정한 객체로 받아오려니까
"System Error : org.springframework.web.client.RestClientException: Error while extracting response for type [] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<>` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]"
이런 에러가 났다. 가져온 데이터를 내가 설정한 모델에 연결시켜줄 수 없다는 메시지인듯?
보내주는 데이터와 받아올 때 데이터 객체 타입이 안맞는다. 그럼 어떻게 하지?
from http://eunbc-2020.tistory.com/224 by ccl(A) rewrite - 2021-09-07 12:00:56