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万+

跨厂商网络设备的RADIUS认证:Windows NPS的兼容性与最佳实践

本文详细探讨了Windows NPS在多厂商网络环境中的RADIUS认证实践,特别针对华为、华三、思科等主流设备的兼容性问题提供解决方案。从基础配置到高级优化,涵盖VSA属性映射、认证协议选择及安全加固措施,帮助企业实现统一身份认证并提升网络安全性。

weixin_29213827的博客 420

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

how2heap 深入学习(1)

how2heap glibc 2.23学习

CoLin的pwn小屋 3177

newman的使用

Newman是一个基于Node.js的postman命令行工具,用于运行和测试PostmanAPI 请求的集合。它允许用户在命令行环境中执行Postman的JSON文件,从而实现接口自动化测试和持续集成,同时支持用户进行自动化测试、批量执行请求、生成报告等操作。

m0_62825058的博客 1615

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

在Mac上搭建本地AI工作流:Dify与DeepSeek的完美结合

本文介绍了在Mac上搭建本地AI工作流的方法,结合Dify平台和DeepSeek模型。Dify是一个开源的大语言模型应用开发平台,支持可视化AI应用开发。部署步骤包括:1)安装Homebrew和Docker;2)克隆Dify代码库;3)配置环境变量并启动服务。同时,使用Ollama工具本地部署DeepSeek模型,将其集成到Dify中创建聊天助手。通过配置模型接口和新建应用,用户可以在本地环境中实现AI对话功能。这种方案既保证了数据隐私,又提供了灵活的AI应用开发能力。

Linda的自动化测试之旅 1534

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

华三R4900 G5服务器实战:用PMC P460-B4阵列卡配置RAID1+RAID6,并搞定CentOS 7.9安装(含热备盘设置)

本文详细解析了在华三R4900 G5服务器上使用PMC P460-B4阵列卡配置RAID1+RAID6混合阵列的全过程,包括热备盘设置和CentOS 7.9系统安装。通过实战步骤和性能优化建议,帮助企业IT人员高效部署高可用存储方案,特别适合需要数据冗余和系统稳定性的企业级应用场景。

weixin_29248653的博客 357

基于FPGA的PCIe核系列:FPGA root模式生成工程例子分析(1)

一、 PCIE系统架构 1.1 PCIE分层结构 PCIE结构 1.2 TLP包 四种TLP类型。 Memory (内存空间操作) IO (IO空间操作) Configuration (配置操作) Message (信息操作) 四种类型TLP,如果有需要响应,称为Non-Posted TLP,如果没有需要响应,称为Posted TLP。 TLP包类型 1.2.1 TLP结构 TLP数据格式 1.2.2 Header各个字段的含义 Header格式 ...

huangshanhu的博客 3432

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

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

weixin_33937913的博客 8043

电阻抗成像肺功能测试数据分析与直方图生成

本文介绍了基于MATLAB的电阻抗成像(EIT)肺功能测试数据分析方法。研究针对32×32像素的EIT图像序列,通过三步算法处理:首先计算特定时间点与谷值图像的差值,然后分析1秒后峰值与1秒值的差值,最终计算比值矩阵并处理异常值。系统展示了数据预处理、关键时间点检测、比值计算及统计分析的全流程,包括生成概率分布直方图和空间分布图等可视化方法。该分析为无创肺功能评估提供量化指标,可应用于临床呼吸监测和疾病诊断。

huanghm88的专栏 197

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

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

Cey_Tao的博客 3033

PCL RANSAC拟合空间圆【2026最新版】

RANSAC三维点云空间3D圆拟合。博客长期更新,本文最新更新时间为:2026年4月13日。代码在PCL1.15.0中测试通过

点云侠的博客 7452

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

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

记录点滴 3247

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

springboot Could not find acceptable representation 错误

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

胡海龙 2199

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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、付费专栏及课程。

余额充值