w11467的gravatar头像
w11467 2017-07-14 14:23:00

SpringMVC+Jackson为什么抛出406错误?

Spring:4.3.8
​Jackson:2.8.8

HTML报的错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.  

异常抛出的位置:

AbstractMessageConverterMethodProcessor类中的
if (outputValue != null && producibleMediaTypes.isEmpty()) {
   throw new IllegalArgumentException("No converter found for return value of type: " + valueType);
} 其中:outputValue:LoginReply@6431 producibleMediaTypes: size=4

SpringMvc配置

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"
      p:prefix="/WEB-INF/jsp/"/>
<context:component-scan base-package="com.fresh.web"/>
<!-- Enables the Spring MVC @Controller programming model -->
<!--<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">-->
    <!--<property name="converters">-->
        <!--<list>-->
            <!--<bean class="com.fresh.conversion.CustomDateConverter"/>-->
        <!--</list>-->
    <!--</property>-->
<!--</bean>-->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>

Jackson依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>

Controller:

@RequestMapping(value = "/log.html", method = RequestMethod.POST)
    public ResponseEntity<LoginReply>  checkUser() {
        LoginReply loginReply = new LoginReply();
        loginReply.setTest("123");
        System.out.println("执行");
        return ResponseEntity.ok(loginReply);
    }

异步请求:

var data = 'username=' + $('#username').val() + "&";
data += 'password=' + $('#password').val();
    $.ajax('/user/log.html',{
        type: "POST",
        data: data,
        timeout: 100000,
        success: function (result) {
            alert(result);
        }
    });
所有回答列表(2)
最代码官方的gravatar头像
最代码官方  LV168 2017年7月14日

406是请求参数不合法,或者参数缺失的问题。

发下最代码的springmvc和jackson的maven pom.xml配置

<!-- For JSON -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.3</version>
        </dependency>
mySong的gravatar头像
mySong  LV11 2017年7月14日

是不是你前台的ajax里面少了那个 contentType 这个参数配置哦,是json的那个

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友