Spring @RequestBody json反序列化 - 不支持的媒体类型 [英] Spring @RequestBody json deserialization - Unsupported Media type

查看:1104
本文介绍了Spring @RequestBody json反序列化 - 不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring 4.1.0获得着名的Json反序列化异常.RELEASE:

I am getting the famous Json deserialization exception with spring 4.1.0.RELEASE:

org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application / json; charset = UTF-8'不支持

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

只有通过几个类似的线程后,我仍然无法找到问题的原因

Only that after pouring through several similar threads, I am still not able to find the cause of the issue

这是我的控制器:

@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public  @ResponseBody String myMethod( @RequestBody MyObj request) {

    .....
}

POJO:

public class MyObj implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

Integer something;  
Integer[] somethingElse;

// Getters and setters

}

我确实有杰克逊依赖:

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.6</version>
    </dependency>

我的POM中的

in my POM

我发出以下请求:

{
something:4,
somethingElse:[1,2]
}

{ "something" : 4, "somethingElse": [1,2] }

我检查了我的JSON格式正确,并且它在我的服务器上点击了相应的控制器。

I checked that I have a properly formatted JSON and its hitting the appropriate controller on my server.

现在可能是什么出错了????

Now what could be going wrong????

编辑:
配置中的这一变化解决了问题:

This change in the config fixed the problem:

在我的根配置中

<bean id="jacksonMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper" />
    <property name="supportedMediaTypes">
        <list>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="application" />
                <constructor-arg index="1" value="json" />
                <constructor-arg index="2" value="UTF-8" />
            </bean>
        </list>
    </property>
</bean> 
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">

在servlet配置中

In servlet config

<mvc:annotation-driven> 
    <mvc:message-converters register-defaults="false">       
       <beans:ref bean="jacksonMessageConverter"></beans:ref>
   </mvc:message-converters>
</mvc:annotation-driven>

在POM中,添加最新版本的
jackson-mapper-asl,
jackson-core,
jackson-databind,
jackson-annotations

In POM, add latest versions of
jackson-mapper-asl, jackson-core, jackson-databind, jackson-annotations

谢谢大家

推荐答案

您是否禁用了默认的MessageConverter?似乎在您的配置中缺少MappingJackson2MessageConverter。

Have you disabled the default MessageConverter? It seems that in your config the MappingJackson2MessageConverter is missing.

你能提供你的mvc配置吗?

Can you provide your mvc config?

这篇关于Spring @RequestBody json反序列化 - 不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆