post xml to Spring REST server returns Unsupported Media Type
- by Mayra
I'm trying to create a simple spring based webservice that supports a "post" with xml content.
In spring, I define an 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 a jaxb based xml 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 class autogenerated by jaxb:
@RequestMapping(method = POST, value = "/resource")
public Resource createResource(@RequestBody Resource resource) {
// do work
}
The result of a webservice call is always "HTTP/1.1 415 Unsupported Media Type". Here is an example 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 correct media type everywhere possible. Anyone have an ideas?