PDA

View Full Version : Issue using SimpleXmlHttpMessageConverter



dutchman_mn
Jun 1st, 2011, 12:49 PM
I am attempting to use the SimpleXmlHttpMessageConverter to render an XML response to a Java object like this:



HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

RestTemplate restTemplate = new RestTemplate(requestFactory);

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());

SimpleXmlHttpMessageConverter xmlMessageConverter = new SimpleXmlHttpMessageConverter();
ArrayList supportedMediaTypes = new ArrayList();

MediaType mediaType = new MediaType("text", "xml", Charset.forName("ISO-8859-1"));
supportedMediaTypes.add(mediaType);
xmlMessageConverter.setSupportedMediaTypes(support edMediaTypes);

messageConverters.add(xmlMessageConverter);
restTemplate.setMessageConverters(messageConverter s);

HttpHeaders headers = new HttpHeaders();

ResponseEntity responseEntity =
restTemplate.exchange(restURL,
HttpMethod.GET,
new HttpEntity<String>(headers),
Class.forName(com.acme.Feed));


The reason I am altering the supported media type was the first time I tried it, I got an error message stating it did not support text/xml. SimpleXmlHttpMessageConverter has a default character set of UTF-8, but my response is text/xml;charset=ISO-8859-1 so I thought that was maybe the problem.

The error message is: org.springframework.web.client.RestClientException : Could not extract response: no suitable HttpMessageConverter found for response type [com.acme.Feed] and content type [text/xml;charset=ISO-8859-1]

The response xml looks like this:



<feed>
<feedname>Alerts</feedname>
<authData>
<token>...</token>
<username>...</username>
<employeeId>...</employeeId>
</authData>
<dataMessages>
<dataMessage>
...
</dataMessage>
</dataMessages>
</feed>


I am at a bit of a loss to understand how to resolve this issue.

Roy Clarkson
Jun 8th, 2011, 03:58 PM
Hi dutchman_mn. I haven't seen this issue, but I also have not tried using an alternate character set with the Simple message converter. I'll take a look and see if there is anything that could be preventing this from working. Have you tried Simple with your example, outside of RestTemplate to confirm that it is marshaling correctly? The exception makes me think this is an issue with RestTemplate though.

Created a JIRA for this:

https://jira.springsource.org/browse/ANDROID-37

dutchman_mn
Jun 10th, 2011, 06:26 AM
No, I have not.

Roy Clarkson
Jun 10th, 2011, 08:52 AM
How is your Feed object annotated? Specifically, do you have the @Root annotation present? Currently, the SimpleXmlHttpMessageConverter's canRead() method is checking for the presence of this annotation. When I remove the @Root annotation from a working sample in my testing, I receive the same RestClientException you described. Simple's docs state that the @Root is required for serializing (writing), but it doesn't appear to be required for reading. So this check in the canRead method can probably be removed. I'll note this in the jira.

Also, as a note, the simple message converter sets application/xml, text/xml, and application/*+xml as default media types. I understand you are trying to get it to work, but you wouldn't need to manually set that under normal usage.

dutchman_mn
Jun 14th, 2011, 06:49 AM
Yes, on the topic of the message converter, I knew that but at the time I was starting to grasp at straws. I had read the same thing you did on @Root and did not put it in. As you did, I should have put it in and given that a shot also.

Roy Clarkson
Jun 14th, 2011, 10:04 AM
I've pushed a few changes for the Simple message converter. I removed the requirement for the @Root annotation on the canRead() method. Additionally, alternate media types were not being set correctly in the InputStreamReader and OutputStreamWriter passed to the Simple Serializer, when reading and writing. I also added a few unit tests checking reading and writing ISO-8859-1 sources. The build snapshots are not exactly stable at the moment, but any confirmation of this working that you can provide, will be helpful in regards to the upcoming M4 release. Thanks.