Spring4.0 MVC request json datagram 406 error resolution

  • 2020-05-27 05:40:55
  • OfStack

Spring4.0 MVC request json datagram 406 error, how to resolve?

Solution 1:

1. Import jackson-core-2.5.1.jar and jackson-databind-2.5.1.jar

2. Spring configuration file addition:


<!--  avoid IE perform AJAX when , return JSON Download file appears 
spring3 To: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4 To: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
 -->  
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  <property name="supportedMediaTypes">  
    <list>  
      <value>text/html;charset=UTF-8</value>  
    </list>  
  </property>  
</bean>  
 
<!--  Start the Spring MVC Annotation function to complete requests and annotations POJO The mapping of  -->  
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  <property name="messageConverters">  
    <list>  
      <ref bean="mappingJacksonHttpMessageConverter" /><!-- json converter  -->  
    </list>  
  </property>  
</bean>  

Solution 2:

1. Import the fastjson package of the third party (alibaba), fastjson-1.2.7.jar

2. Add Spring configuration file:


<mvc:annotation-driven> 
  <mvc:message-converters register-defaults="true"> 
    <!--  avoid IE perform AJAX when , return JSON Download file appears  --> 
    <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 
      <property name="supportedMediaTypes"> 
        <list> 
          <value>application/json;charset=UTF-8</value> 
        </list> 
      </property> 
    </bean> 
  </mvc:message-converters> 
</mvc:annotation-driven> 

Related articles: