I'm using springframework -RestTemplate, in order to make get request , and convert the xml response to java object. After the operation : RestTemplate.exchange, I got the following exception:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not instantiate value of type [simple type, class Order] from
The response from the client in XML, and I added it to the headers:
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
So why it's trying to parse the response as JSON? and how can I resolve it?
Thanks!!
The most common cause is you are getting some XML which does not conform to the deserialization rules defined in your model (or maybe it is malformed XML).
Other possible cause is your RestTemplate
missing a message converter capable of dealing with XML conversion. By default, Spring Boot configures a Jaxb2RootElementHttpMessageConverter
, but only if you have JAXB2 in your classpath, so you should check this dependency is available to your project.
You can print which message converters are registered in your RestTemplate
, and which media types they accept, with the following code:
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
System.out.println("Converter: " + converter.getClass().getSimpleName() + ", supports: "
+ converter.getSupportedMediaTypes().toString());
}