Spring MVC:@ResponseBody,415 不支持的媒体类型
我在将 JSON Post 映射到特定 Java 对象以通过 Hibernate 保存它时遇到问题
Ajax 调用的标头设置正确...
Accept application/json
Content-Type application/json; charset=UTF-8
并且 HTTP 方法是 POST >
这是我的配置...
看起来像这样
@RequestMapping(value = {"/save.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj,Model model) {
...
}
我的 Spring MVC 函数映射在我的容器 xml 中
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/assets/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
我在我的容器 xml 中有一个 ContentNegotiatingViewResolver 像这样 jackson 的部分
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer"
init-method="init">
<property name="messageConverters">
<list>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
>
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
我有
- jackson-core-asl-1.8 .5
- jackson-mapper-asl-1.8.5
在我的 /lib 文件夹中
,Jackson 适用于这样的简单情况
public class Simple {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@RequestMapping(value = {"/saveSimple.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Simple simple,Model model) {
...
}
,当我使用curl 测试它时
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1}' http://<URL>/saveSimple.json
没有问题,但如果我使用 Hibernate 对象测试它,我会收到任何想法的消息
415 Unsupported Media Type
。
I'm having trouble mapping a JSON Post to a particular Java Object to save it via Hibernate
Headers of the Ajax call are correctly set...
Accept application/json
Content-Type application/json; charset=UTF-8
and the HTTP-Method is POST
Here comes my configuration...
My Spring MVC function mapping looks like this
@RequestMapping(value = {"/save.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Seizure seizureObj,Model model) {
...
}
in my container xml I have a ContentNegotiatingViewResolver like this
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/assets/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
in my container xml the part for jackson
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer"
init-method="init">
<property name="messageConverters">
<list>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
>
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
I have
- jackson-core-asl-1.8.5
- jackson-mapper-asl-1.8.5
in my /lib folder
and Jackson works for a simple case like this
public class Simple {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@RequestMapping(value = {"/saveSimple.json"},method = RequestMethod.POST)
public ModelMap save(@RequestBody Simple simple,Model model) {
...
}
when I test it with curl
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1}' http://<URL>/saveSimple.json
No problems, but if I test it with the Hibernate object I get the message
415 Unsupported Media Type
any ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StanMax 引导我走向正确的方向...
首先,我必须正确设置
@JsonManagedReference
和@JsonBackReference
。我将 ManagedReference 设置为表示 ONE-to-MANY 关系中的 ONE 的属性,但显然您必须将其设置为 MANY strong> 属性(集合)请参阅此处的描述
http://wiki.fasterxml.com/JacksonFeatureBiDirReferences
我的例子中的问题是我没有在 JSON POST 中发送足够的信息来匹配 Hibernate 要求。
这导致了一个异常,即从我的 JSON POST 创建 Hibernate 对象的正确字段不存在。
我创建了一个测试 URL,我自己在其中创建了对象映射器,并尝试手动反序列化 JSON。这会抛出正确的错误消息。
在我看来,Spring 不幸地抛出了 415“不支持的媒体类型”异常,这有点误导。
所以为了未来。首先手动尝试一下(请参阅随附的示例),然后继续。
希望这对某人有帮助。
问候 JS
StanMax lead my in the right direction...
First I had to set the
@JsonManagedReference
and@JsonBackReference
right. I set the ManagedReference to the attribute which represents the ONE of of the ONE-to-MANY relationship, but apparently you have to set it to the MANY attribute (the collection)see the description here
http://wiki.fasterxml.com/JacksonFeatureBiDirReferences
The problem in my case was that I didn't send sufficient information in my JSON POST to match the Hibernate requirements.
This resulted in an Exception that the proper fields to create a Hibernate object out of my JSON POST, were not present.
I created a test url where I created the Object mapper myself and tried to de-serialize JSON by hand. This throw the right error message.
It seems to my that Spring unfortunately throws the 415 "Unsupported Media Type" exception which is a bit misleading.
So for the future. Try it out by hand first (see example included) and than go on.
Hope this helps someone.
Regards JS
您可以尝试的一件事是弄清楚问题是否出在 Jackson 的数据绑定(无法序列化休眠对象)或其他问题上。这可以通过手动构造 ObjectMapper,检查“mapper.serializeValueAsString()”是否有效或引发异常来完成。
这很可能与 Jackson 对延迟加载属性的处理有关;如果是这样,您可能需要使用 jackson hibernate 模块 因为它可以处理 Hibernate 特定的详细信息。
One thing you can try is to figure out if the problem is with Jackson's data binding (fails to serialize hibernate object) or something else. This can be done by manually constructing ObjectMapper, checking to see if 'mapper.serializeValueAsString()' works or throws an exception.
There is a good chance it has to do with Jackson handling of lazily-loaded properties; if so, you may need to use jackson hibernate module as it can handle Hibernate-specific details.
我也有同样的问题。在阅读了 MappingJacksonHttpMessageConverter 的工作原理后,我意识到我没有正确初始化它。
所以我在我的rest-servlet.xml中使用了以下内容并且它起作用了..
I had the same problem. After reading through how MappingJacksonHttpMessageConverter works I realized that I had not initialized it properly.
So I used the following in my rest-servlet.xml and it worked..
我遇到了类似的问题,这是由于我的一个类有一个具有非原始键类型的映射。我能够 @JsonIgnore 到这个字段来解决这个问题。
I had a similar problem, which was due to the fact that one of my classes had a map with a non-primitive key type. I was able to @JsonIgnore to this field to work around the problem.