Spring Boot: HttpMediaTypeNotAcceptableException: Could not find acceptable representation原因及解决方法

SpringMVC项目使用ajax向服务器请求数据时出现HttpMediaTypeNotAcceptableException错误的解决办法 Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not findacceptable representation 这时候前台一般也会出现406错误 这个错有很多出现错误的场景,网上有很多相关解决办法,但是很多都是用的老版本,对新版本的springMVC不怎么管用,而且有的说修改pom.x... 阅读详情

错误场景

使用Spring Boot的Web项目,在其 resources/static/目录下存在login.html静态文件,同时还有一个处理/login请求的控制器方法(该方法会返回JSON格式的数据)。此时如果访问localhost:8080/login.html,用户期望返回login.html页面,但框架却报错:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:235) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:382) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:322) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:60) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:351) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]

错误原因

Spring Boot的MVC默认配置中使用的 ViewResolver 为 ContentNegotiatingViewResolver,该视图解析器的功能是根据要请求的文档类型,来查找不同的视图以返回对应格式的文档。请求的文档类型要可以从请求头中的Accept中获取,也可以通过URI后缀名得到,如/login.html即为请求HTML格式的文档,这两种方式分别对应着两种不同的Strategy(策略),默认为根据URI后缀名。Spring Framework Reference 17.5 节 ContentNegotiatingViewResolver中有说明:

The ContentNegotiatingViewResolver does not resolve views itself but rather delegates to other view resolvers, selecting the view that resembles the representation requested by the client. Two strategies exist for a client to request a representation from the server:
• Use a distinct URI for each resource, typically by using a different file extension in the URI. For example, the URI http://www.example.com/users/fred.pdf requests a PDF representation of the user fred, and http://www.example.com/users/fred.xml requests an XML representation.
• Use the same URI for the client to locate the resource, but set the Accept HTTP request header to list the media types that it understands. For example, an HTTP request for http:// www.example.com/users/fred with an Accept header set to application/pdf requests a PDF representation of the user fred, while http://www.example.com/users/fred with an Accept header set to text/xml requests an XML representation. This strategy is known as content negotiation.

因此,当用户请求 /login.html 时,spring会查找/login对应的控制器,并得到其返回的文档类型为application/json, 然后判断它与后缀名.html文档类型是否匹配,如果不匹配,就报HttpMediaTypeNotAcceptableException了。
其实它的初衷是好的,它是想实现访问/user.json时返回JSON数据,访问/user.html返回HTML, 访问/user.xml则返回XML的功能。但是在这里我们只用Spring Boot提供RESTful接口,因此该功能就无用武之地了。

解决方案

  • 防止静态文件名跟控制器请求路由冲突。如本例中,将login.html更名为signin.html。
  • 将静态文件URI与动态请求URI分离。如,把提供REST接口的URI都改成以/api/XXX开头,把静态文件改为以/static/XXX开头。这样当请求/static/login.html时,spring会直接使用内置的处理静态资源的控制器返回静态文件而不再去查找用户定义的控制器、
正确解决org.springframework.web.HttpMediaTypeNotAcceptableException异常的有效解决方法 阅读详情

相关推荐

安卓开发:com.ashokvarma.bottomnavigation.BottomNavigationBar,点击效果如何去掉?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1236

Could not find acceptable representation(已解决)

状态码报错406,Could not find acceptable representation(已解决)

qq_45864365的博客 7万+

串级PID 位置环+速度环

串级PID 位置环+速度环

weixin_62775618的博客 9109

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio

不知道你有没有碰到上面这个错误。本文会根据工作中碰到的该异常,梳理异常产生的原因。知道了原因,那么解决的办法也就很简单了。 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation at org.springframework.web.servle...

sdlyjzh的专栏 9441

CANoe.Diva实战:基于CDD的UDS诊断自动化测试全流程解析

本文详细解析了使用CANoe.Diva进行基于CDD的UDS诊断自动化测试的全流程,包括环境搭建、CDD文件配置、测试范围设置、用例生成与验证、CANoe集成测试及报告分析。通过实战经验分享,帮助工程师快速掌握诊断自动化测试技巧,提升测试效率与质量。

weixin_29258297的博客 218

手把手教你用STM32实现三相永磁同步电机FOC控制(附代码)

本文详细介绍了基于STM32的三相永磁同步电机(PMSM)FOC控制实现方法,涵盖硬件设计、软件架构、参数调试及完整代码解析。通过磁场定向控制(FOC)算法,实现高效精准的电机控制,适用于工业自动化、电动汽车等领域。文章还提供了硬件选型建议、电流采样方案对比及PID参数整定技巧,助力开发者快速掌握FOC控制技术。

weixin_29168675的博客 94

76. Spring Boot完美解决(406)Could not find acceptable representation原因及解决方法

【原创文章】 使用Spring Boot的Web项目,处理/login请求的控制器方法(该方法会返回JSON格式的数据)。此时如果访问localhost:8080/login.html,用户期望返回jsons数据,但框架却报错: There was an unexpected error (type=Not Acceptable, status=406). ...

diuju8472的博客 5491

安装 Claude Code 并接入阿里云百炼(Bailian)

这些工具通常提供可视化界面,只需在设置中填入阿里云百炼的 API Key 和 Base URL 即可,无需配置环境变量。注意:如果你使用的是阿里云百炼的 Coding Plan 套餐,请使用该套餐专属的 API Key 和 Base URL。你可以使用文本编辑器打开该文件,或者在终端使用命令(如 macOS/Linux 下使用 nano ~/.claude/settings.json)。你可以直接用自然语言下达指令,例如:“帮我在这个目录下写一个 Python 脚本,实现两数之和的功能”。

hopsky的博客 1841

HttpMediaTypeNotAcceptableException Could not find acceptable representation

1.项目背景:Springboot+SpringMVC+Spring+Mybatis,上传文件 2.问题:上传文件时,报错406,HttpMediaTypeNotAcceptableException Could not find acceptable representation 3.js代码片段: 4.请求头: 5.后台代码片段: 6.解决方案: Application...

小明的博客 3815

从零开始,手把手教你用RTL代码设计一个简单的8位CPU(附状态图与ALU设计)

本文详细介绍了如何从零开始设计一个简单的8位CPU,包括状态机设计、ALU实现与RTL编码实战。通过具体的指令集分析、状态图设计和Verilog代码示例,帮助读者深入理解CPU工作原理,并掌握RTL代码编写技巧。特别适合计算机系统学习者和硬件设计爱好者。

weixin_30847063的博客 320

springboot+vue项目基础开发(12)vue项目的搭建

在src目录下新建 api,utils,views文件夹。删除componets目录下自动生成的所有文件。在utils文件夹下创建request.js。启动项目,记得是要在管理员下启动。

m0_58498702的博客 152

Could not find acceptable representation原因及解决方法

tags: 解决错误, title: Could not find acceptable representation原因及解决方法 Spring Boot: HttpMediaTypeNotAcceptableException: Could not find acceptable representation原因及解决方法: 今天在写项目的时候出现了这个问题,上网查了一下资料。主要是以下两...

weixin_33937913的博客 8043

6.4 直流无刷电机(BLDC):测试与性能评估

本文系统阐述了直流无刷电机(BLDC)的测试与性能评估方法,涵盖参数测量、性能测试、测试平台设计及可靠性评估。详细介绍了绕组电阻、电感和反电动势的测量原理与修正方法,以及转矩-转速特性、效率分析和动态响应测试的技术要点。通过搭建高精度测功机系统,结合自动化数据处理,实现对电机性能的全面评估。最后讨论了温升、振动、EMC等特殊测试方法,为BLDC电机的设计验证和质量控制提供了完整的技术路径。

技术工作者 439

SpringBoot 项目 Could not find acceptable representation 解决方法

Controller 方法返回的对象没有 get 方法,无法将对象转换为 JSON。可以使用 lombok 库在类上注解 @Data 或者 @Getter。添加 get 方法,有 get 方法的属性才会转换称 JSON。

Cey_Tao的博客 3033

Agentic AI应用架构师如何提升AI应用的灵活性

可扩展性(Scalability):快速添加新功能或扩展任务范围(如Agent从“客服”扩展到“供应链管理”);可修改性(Modifiability):调整Agent的决策逻辑或动作策略,无需重构整个系统;鲁棒性(Robustness):在环境异常或数据噪声下保持性能稳定;自适应(Self-Adaptation):自主学习新场景,无需人工干预。状态空间(S):Agent能感知的所有内部/外部状态(如用户需求、环境数据);动作空间(A):Agent能执行的所有动作(如调用工具、发送消息);

项目管理的博客 576

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable ···

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation] 这种报错原因:类中没有getter和setter方法造成的。。。。。。。。。。。。。

记录点滴 3247

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

springboot Could not find acceptable representation 错误

报错内容如下: springboot org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 错误原因: 在springboot返回给前端数据内容时,如果内容时实体类需要专为json返回去,这里需要实体类有getter和setter方法,所以错误原因就是由于没有给返回给前端的实体类写getter/setter方法导致,加上后不报错 ...

胡海龙 2199

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Spring boot2 导出word 报错 HttpMediaTypeNotAcceptableException: Could not find acceptable representation

Spring boot2.0 导出word 报错 HttpMediaTypeNotAcceptableException: Could not find acceptable representation   将spring boot1.5 升级到2.0以后开始报错. 区别: 1.5 @Configuration @EnableSwagger2 public class SwaggerC...

niuzaiwenjie的专栏 3310

SpringBoot导出excel数据报错Could not find acceptable representation

今天在实现后台导出数据时,导出数据一直报错: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 在导出excel数据时,返回的数据时文件流的格式,写入到response.getOutputStream()中的,返回的数据前端下载是没有问题的,但是后端就是有报错,最后在仔细检查才发现,是因为我在Controller类上使用了注解@RestC..

IntialJ的博客 1723

网页406 idea Could not find acceptable representation

最近在搞微服务项目,所有工程都搭建完成,启动没问题,但是访问接口的时候 There was an unexpected error (type=Not Acceptable, status=406). Idea --->Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable represent.

qq_24981615的博客 862

JAVA-nacos微服务内部调用报错异常:HttpMediaTypeNotAcceptableException: Could not find acceptable representation

nacos微服务内部调用报错异常:HttpMediaTypeNotAcceptableException: Could not find acceptable representation

weixin_46690278的博客 645

No acceptable representation/Could not find acceptable representation

在写前后端分析的Spring Boot项目的登录功能时,因为自定义了返回结果封装类Result,其中包含了自定义的字段,但没有提供这些字段的getter和setter方法,提供相应字段的getter和setter方法,如果使用了lombok直接加上@Data注解即可。

L_Y_H110216的博客 322

org.springframework.web.HttpMediaTypeNotAcceptableException:Could not find acceptable represe

无法接受的类型,可能是序列化的时候失败了。

Miss Dan的博客 3665

ypeNotAcceptableException: Could not find acceptable representation]

2022-04-18 20:59:54.007 WARN 35872 — [nio-8081-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation] 我个人是这个原因 我代码返回的 数据 return ReturnT.success();

jonathan_joestar的博客 572
上一篇: OS X 10.11下词法分析工具Flex编译链接问题
下一篇: Spring Cloud与分布式系统
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值