2017-03-29 1 views
0

서비스를 사용하기 위해 restTemplate을 사용하고 있습니다.RestTemplate이 APPLICATION_FORM_URLENCODED 및 Request Pojo와 함께 작동하지 않습니다.

HttpHeaders headers = new HttpHeaders(); 
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 
HttpEntity request = new HttpEntity(countryRequest, headers);     
CountryResponse response = restTemplate.postForObject(countryURL, request, CountryResponse.class); 

countryRequest 그냥 문자열 필드 code와 POJO의 객체입니다. restTemplatejackson2HttpMessageConverter이고 FormHttpMessageConvertermessageConverters입니다.

나는 다음과 같은 예외가 점점 오전 :

org.springframework.web.client.RestClientException: 
    Could not write request: no suitable HttpMessageConverter found for request type [CountryRequest] and content type [application/x-www-form-urlencoded] 

을하지만 대신 CountryRequestMultiValueMap를 사용하는 경우, 내가 200 응답 가지고 :

MultiValueMap<String, String> map= new LinkedMultiValueMap<>(); 
map.add(code, "usa"); 
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers); 

여기 MultiValueMap 접근 방식을 대체 할 수있는 방법이 있나요을 ?

답변

0

요청 직렬화에는 두 가지가 있습니다. 일]적인 FORM 데이터 또는 JSON 오브젝트입니다. 양식 데이터는 POST 페이로드에서 문자열의 간단한 키 - 값 쌍을 보내는 가장 간단하고 오래된 방법입니다. 하지만 중첩 된 속성이나 일부 목록 또는지도가있는 개체를 보내야하는 경우 문제가됩니다. 그래서 모든 사람들이 POJO 객체로 더 쉽게 deserialize 될 수있는 JSON 형식을 사용하려고합니다. 그리고 이것은 현대 웹에 대한 사실상의 표준입니다.

어떤 이유로 인해 RestTemplateCountryRequest을 직렬화하려고 시도하지만 형식 데이터로 직렬화하는 방법을 알지 못합니다. 당신이 전송되는 POJO로 request를 교체하십시오 :

CountryRequest request = new CountryRequest();     
CountryResponse response = restTemplate.postForObject(countryURL, request, CountryResponse.class); 

다음 RestTemplate은 JSON에 CountryRequest을 (기본 동작 인) 직렬화하려고합니다.

관련 문제