I am trying to create a simple spring based web service that supports a "message" with xml content.
In spring, I define AnnotationMethodHandler:
<bean id="inboundMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <property name="marshaller" ref="xmlMarshaller"/> <property name="unmarshaller" ref="xmlMarshaller"/> </bean> </util:list> </property> </bean>
And jaxb based marshaller:
<bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="contextPaths"> <array> <value>com.company.schema</value> </array> </property> <property name="schemas"> <array> <value>classpath:core.xsd</value> </array> </property> </bean>
My controller is annotated as follows, where Resource is a jaxb auto generated class:
@RequestMapping(method = POST, value = "/resource") public Resource createResource(@RequestBody Resource resource) {
The result of the webservice call is always the "HTTP / 1.1 415 Unsupported Media Type". Here is an example of a service call:
HttpPost post = new HttpPost(uri); post.addHeader("Accept", "application/xml"); post.addHeader("Content-Type", "application/xml"); StringEntity entity = new StringEntity(request, "UTF-8"); entity.setContentType("application/xml"); post.setEntity(entity);
It seems to me that I am setting the right type of media everywhere. Does anyone have any idea?
Edit: after further debugging, it looks as if it does not come to an attempt to untie the object. I don't quite understand the black magic behind how AnnotationMethodHandler knows that something like application / xml should go into the MarshallingHttpConverter. Can anyone shed some light on this?
java spring rest spring-mvc jaxb
Cheryl Simon
source share