Spring Boot 启动项目返回对象时 页面 spring boot type=Not Acceptable, status=406
来源:互联网 发布:linux 设置文件夹权限 编辑:程序博客网 时间:2024/06/05 16:24
错误原因:
Spring Boot的MVC默认配置中使用的 ViewResolver 为 ContentNegotiatingViewResolver,该视图解析器的功能是根据要请求的文档类型,来查找不同的视图以返回对应格式的文档。请求的文档类型要可以从请求头中的Accept中获取,也可以通过URI后缀名得到,如/login.html即为请求HTML格式的文档,这两种方式分别对应着两种不同的Strategy(策略),默认为根据URI后缀名。
因此,当用户请求 /login.html 时,spring会查找/login对应的控制器,并得到其返回的文档类型为application/json, 然后判断它与后缀名.html文档类型是否匹配,如果不匹配,就报HttpMediaTypeNotAcceptableException了。
其实它的初衷是好的,它是想实现访问/user.json时返回JSON数据,访问/user.html返回HTML, 访问/user.xml则返回XML的功能。但是在这里我们只用Spring Boot提供RESTful接口,因此该功能就无用武之地了。
解决方案
我们刚才在上面说了Spring 会通过URI后缀获取请求格式,当访问/login.html的时候,那么根据当前的URI获取到后缀.html,那么判断与.html文档类型是否匹配,匹配的话执行相应的解析器。那么我们就会想,我们能够关闭这种默认的后缀匹配规则呢,既然本文章说是完美解决答案就是肯定的。解决步骤就两步骤:
(1)在启动类App.java类中继承:WebMvcConfigurerAdapter
(2)覆盖方法:configureContentNegotiation
具体代码如下:
package com.kfit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter {
/**
(1)在启动类App.java类中继承:WebMvcConfigurerAdapter
(2)覆盖方法:configureContentNegotiation
favorPathExtension表示支持后缀匹配,
属性ignoreAcceptHeader默认为fasle,表示accept-header匹配,defaultContentType开启默认匹配。
例如:请求aaa.xx,若设置<entry key="xx" value="application/xml"/> 也能匹配以xml返回。
根据以上条件进行一一匹配最终,得到相关并符合的策略初始化ContentNegotiationManager
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
public static void main(String[] args) {
SpringApplication.run(ApiCoreApp.class, args);
}
}
这里说下核心代码:
configurer.favorPathExtension(false);
favorPathExtension表示支持后缀匹配,
属性ignoreAcceptHeader默认为fasle,表示accept-header匹配,defaultContentType开启默认匹配。
例如:请求aaa.xx,若设置<entry key="xx" value="application/xml"/> 也能匹配以xml返回。
根据以上条件进行一一匹配最终,得到相关并符合的策略初始化ContentNegotiationManager
- Spring Boot 启动项目返回对象时 页面 spring boot type=Not Acceptable, status=406
- Spring Boot Controller 无法访问 type=Not Found, status=404
- Spring boot启动返回jsp页面
- spring-boot项目启动
- Spring MVC + JSON = 406 Not Acceptable
- Spring MVC 报错:Response Status: 406 (Not Acceptable)
- spring boot项目如何启动
- spring-boot项目启动方式
- spring后台返回json数据 页面406错误 Not Acceptable解决
- Spring Boot---Web应用返回jsp页面
- spring boot security /error页面返回500
- [Spring boot] web应用返回jsp页面
- [Spring boot] web应用返回jsp页面
- [Spring boot] web应用返回jsp页面
- spring boot 返回jsp页面例子
- Spring Mvc 返回Json对象给前端报 406 (Not Acceptable) 问题解决
- Spring SpringMvc 406 Not Acceptable
- spring mvc 406 (Not Acceptable)
- 学数组与指针一点感悟
- Codeforces Round #449 (Div. 2) 897A. Scarborough Fair
- pandas聚合和分组运算之groupby
- 记录百度地图开发(android)百度地图定位返回4.9E-324的解决方案
- mongo-java-driver -3.2.2学习笔记-08-Monitoring
- Spring Boot 启动项目返回对象时 页面 spring boot type=Not Acceptable, status=406
- C语言作业题(3)
- NOIP 2017 Day1 总结
- 怎样查看django的安装路径
- python创建Excel文件 xlwt
- python中pandas.DataFrame对行与列求和及添加新行与列示例
- Git常用命令总结,不定期更新
- windows linux共享文件夹 samba
- scala akka http基本使用