在使用ajax接收返回map的时候,提示406错误

在使用ajax接收返回map的时候,提示406错误

这时因为springMVC3.x和springMVC4.x所以来的jackson的jar包有所改变,我们以maven来说明

在3.x中,为

<pre name="code" class="html">     <artifactId>jackson-mapper-lgpl</artifactId>
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.8.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-lgpl</artifactId> 

<version>1.8.1</version> 

</dependency>

 到4.x中,使用全新的jackson-annotations-2.5.0.jar,jackson-core-2.5.0.jar,jackson-databind-2.5.0.jar 原有的jar不再使用 
 

如下所示

<dependency>
              <groupId>com.fasterxml.jackson.jaxrs</groupId>
              <artifactId>jackson-jaxrs-xml-provider</artifactId>
              <version>2.5.2</version>
</dependency>

<dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-core</artifactId>  
            <version>2.5.2</version>  
</dependency>  

<dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <version>2.5.2</version>  
</dependency>  

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.2</version>
</dependency>
如果修改了jar包以后还报错,另外一个原因可能是springMVC.xml头文件上的xsi:schemaLocation里版本显示的还是3.x,这里手动改成4.x。
如果还是没有解决问题,可能的原因是spring4.x关于json配置方式有所变更

3.x中

<mvc:annotation-driven />
    <!-- 处理请求response返回值,如下配置能正确返回字符串型返回值,如返回值为对象,则自动转为json -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json; charset=UTF-8</value>
                <value>text/html; charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <bean id="mappingStringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter" />

    <bean id="handleAdapter"
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
            </list>
        </property>
    </bean>

4.x中

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="false" />
    </bean>
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>  

    <bean id="stringConverter"  
        class="org.springframework.http.converter.StringHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/plain;charset=UTF-8</value>  
            </list>  
        </property>  
    </bean>  

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                <ref bean="stringConverter" />  
                <ref bean="jsonConverter" />  
            </list>  
        </property>  
    </bean> 

修改后,ajax即可成功接收map信息

如果解决了上面问题

不报406错误了,而报500错误,证明有了作用,结果发现是引入的jackson-databind-2.7版本未知原因冲突,所以替换为2.5版本后,解决了问题.

你可能感兴趣的:(java,Ajax,Web,json,404,406)