使用ajax向後臺請求時,前臺報錯406 Not Acceptable
阿新 • • 發佈:2018-12-19
使用ajax向後臺請求時,前臺報錯406 Not Acceptable
公司專案裡面是springboot,都是整合自動配置好了的,在用xml方式的時候,直接使用@ResponseBody,並不能返回想要的型別,報了406,查了很多,不怎麼管用,終於找到方案,小記一下
- 引入三個jar包(直接貼pom了)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId >
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.3</version>
</dependency >
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.3</version>
</dependency>
- spring配置檔案裡面配置bean
<!-- 避免IE執行AJAX時,返回JSON出現下載檔案 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉換器 -->
</list>
</property>
</bean>
- 後臺controller上的相應ajax請求方法上正常新增@ResponseBody,就能返回想要了型別的返回值了