[英]Spring RestTemplate not working
I have created application in Spring using RestTemplate, Using Rest-Template I am consuming an external webservice which is having a header as Accept as "application/json". 我使用RestTemplate在Spring中创建了应用程序,使用Rest-Template在使用一个外部Web服务,该服务的标头为“ application / json”。 In my Rest-Template I have added the header but still it is giveing me the following exception 在我的Rest-Template中,我已经添加了标题,但是仍然给了我以下异常
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.site.Employee] and content type [application/octet-stream]
My code is as given below 我的代码如下
private static String BASE_URI = "http://localhost:8181/test/employee"
final HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
requestHeaders.setContentType(new MediaType("application", "json"));
final HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
final RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
final ResponseEntity<Employee> responseEntity = restTemplate.exchange(BASE_URI, HttpMethod.GET, requestEntity, Employee.class);
Can anyone please tell me some solution for this 谁能告诉我一些解决方案
Update 1 更新1
When I tries the below code, it is working fine 当我尝试下面的代码时,它工作正常
final ResponseEntity<String> responseEntity = restTemplate.exchange(BASE_URI, HttpMethod.GET, requestEntity, String.class);
Employee employee = new ObjectMapper().readValue(responseEntity.getBody(), Employee.class);
You'll need to add application/octet-stream
to the list of supported mimetypes in MappingJackson2HttpMessageConverter
: 您需要在MappingJackson2HttpMessageConverter
中将application/octet-stream
添加到受支持的mimetypes列表中:
final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.