SpringMVC ajax请求406 错误解决方案

SpringMVC ajax请求406 错误解决方案:

    在springMVC配置文件中添加以下代码:

<!--避免ajax请求出现406错误-->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
       <property name="favorPathExtension" value="false"/>
       <property name="favorParameter" value="false"/>
       <property name="ignoreAcceptHeader" value="false"/>
       <property name="mediaTypes">
              <value>
                     atom = application/atom+xml
                     html = text/html
                     json = application/json
                     * = */*
              </value>
       </property>
</bean>

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" >
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list><value>text/html;charset=UTF-8</value></list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

  如果是maven项目需要在pom.xml 添加如下依赖:(不是maven项目就需要,自行下载咯)

<!--JSON: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-databind</artifactId>
   <version>${jackson-version}</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>${jackson-version}</version>
</dependency>

pom.xml中添加版本信息:

<!-- 版本号 -->
<properties>
   <spring-version>4.1.6.RELEASE</spring-version>
   <org.slf4j-version>1.6.1</org.slf4j-version>
   <!-- JSON:jackson -->
   <jackson-version>2.6.3</jackson-version>
</properties>


你可能感兴趣的:(SpringMVC ajax请求406 错误解决方案)