I get the below exception when trying to call a rest service using RestTemplate. I am using MarshallingHttpMessageConverter with Jaxb2Marshaller
My client code is below..Code:Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.mycompany.server.jaxb.device.ModelType] and content type [application/xml] at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:597) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:391) at com.mycompany.client.rest.DeviceClient.createModel(DeviceClient.java:36) at com.mycompany.client.Main.main(Main.java:13)
And I am pasting my application.xml below..Code:ModelType model = getModel(modelNum, devType, devSubtype); URI url = null; url = new URI("http://localhost:8080/rest/model"); HttpHeaders headers = getHeaders(); HttpEntity<ModelType> reqEntity = new HttpEntity<ModelType>(model, headers); ResponseEntity<String> respEntity = restTemplate.exchange(url, HttpMethod.PUT, reqEntity, String.class); respEntity.getBody();
If my change my cilent code to use HttpEntity<String> instead of HttpEntity<ModelType> everything works fine and I am able to get a response back.Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <bean name="deviceClient" class="com.mycompany.client.rest.DeviceClient"> <property name="restTemplate" ref="restTemplate"/> </bean> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <property name="marshaller" ref="jaxbMarshaller"/> <property name="unmarshaller" ref="jaxbMarshaller"/> </bean> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> </list> </property> </bean> <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>com.mycompany.server.jaxb.device.ModelType</value> </list> </property> </bean> </beans>
The issue seems to be supports method in Jaxb2Marshaller. The 'unmarshaller' in the code below is of type Jaxb2Marshaller and the line this.unmarshaller.supports(clazz) returns false. I do not have code for the Jaxb2Marshaller and could not dig in any further.Code:ModelType model = getModel(modelNum, devType, devSubtype); URI url = null; url = new URI("http://localhost:8080/rest/model"); HttpHeaders headers = getHeaders(); HttpEntity<String> reqEntity = new HttpEntity<String>("hello world", headers); ResponseEntity<String> respEntity = restTemplate.exchange(url, HttpMethod.PUT, reqEntity, String.class); respEntity.getBody();
I will try to find the source code and investigate further. But if anybody knows the solution, any answer is greatly appreciated.Code:@Override public boolean supports(Class<?> clazz) { return this.unmarshaller.supports(clazz); }
Thanks,
Benam


Reply With Quote