java 将 xml 发布到 Spring REST 服务器返回不支持的媒体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2405130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
post xml to Spring REST server returns Unsupported Media Type
提问by Cheryl Simon
I'm trying to create a simple spring based webservice that supports a "post" with xml content.
我正在尝试创建一个简单的基于 spring 的 web 服务,它支持带有 xml 内容的“帖子”。
In spring, I define an AnnotationMethodHandler:
在 spring 中,我定义了一个 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:
还有一个基于 jaxb 的 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:
我的控制器注释如下,其中“Resource”是 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:
Web 服务调用的结果始终是“HTTP/1.1 415 不支持的媒体类型”。这是一个示例服务调用:
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?
在我看来,我在任何可能的地方都设置了正确的媒体类型。有人有想法吗?
Edit: after further debugging, it looks as though it never gets as far as trying to unmarshal the object. I don't quite understand the black magic behind how the AnnotationMethodHandler knows that something of type application/xml should go to the MarshallingHttpConverter. Can anyone shed any light on that?
编辑:经过进一步调试后,它看起来好像从未尝试解组对象。我不太明白 AnnotationMethodHandler 如何知道 application/xml 类型的东西应该转到 MarshallingHttpConverter 背后的黑魔法。任何人都可以对此有所了解吗?
采纳答案by skaffman
The most likely reason is that the JAXB context doesn't know how to unmarshal to a Resourceobject.
最可能的原因是 JAXB 上下文不知道如何解组Resource对象。
Does Resourcehave an @XMLRootElementannotation? If not, then Jaxb2Marshallerwill not accept the parameter, and you'll get the 415 error. This is done by delegation from Sprng to the JAXB runtime, Spring doesn't really have much say in the matter.
是否Resource有一个@XMLRootElement注释?如果不是,则Jaxb2Marshaller不会接受该参数,并且您将收到 415 错误。这是由 Sprng 委托给 JAXB 运行时完成的,Spring 在这件事上没有太多发言权。
edit: The actual coercion of the data on to the @RequestBodyparameter is done in HandlerMethodInvoker.resolveRequestBody(). There are quite a number of conditions that must be met before the match is made, including matching of MIME type and parameter class type, and if it fails, there's no logging, just the HTTP 415. Have a look at the source for that method, and better yet, do some remote debugging to see where the logic is failing for your setup.
编辑:数据对@RequestBody参数的实际强制是在HandlerMethodInvoker.resolveRequestBody(). 在进行匹配之前,必须满足很多条件,包括匹配 MIME 类型和参数类类型,如果失败,则没有日志记录,只有 HTTP 415。查看该方法的来源,更好的是,进行一些远程调试以查看逻辑在您的设置中失败的位置。

