HTTP Status 406 – Not Acceptable The target resource does not have a current representation that

ゝ一纸荒年。 2021-09-21 15:30 384阅读 0赞

博客来源:

项目中 新建 HTML页面 然后运行项目的时候发现报错,报错如下

根据在请求中接收的主动协商头字段,目标资源不具有用户代理可以接受的当前表示,并且服务器不愿意提供默认表示。

HTTP Status 406 – Not Acceptable


Type Status Report

Description The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.


Apache Tomcat/8.5.37

如下图:多么痛的领悟,不可接受的请求?

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xfZmx5X0o_size_16_color_FFFFFF_t_70

其实提示说的很明显了,请求的目标资源没有被代理,也就是找不到,服务器也没有设定资源访问不到默认的页面,然后才出现这个页面

我的项目是SSM,且说说我的分析思路吧

  • 是否请求CSS、JS时被拦截,查看设置并没有
  • 是否设置拦截时配置出错,经过查看并没有
  • 是否请求格式问题,经过查看还是没有
  • 是否是缓存问题!!重点来了

    1. 清除浏览器缓存,防止请求数据老旧
    2. 请求Tomcat缓存,让其重新加载项目
    3. 清除IDEA缓存,让其重新生成class文件
  • 后面还有一种问题的探讨,即输出与格式的转换问题

果然~费了好大力气,就是缓存的锅,清除缓存重新编译访问正常

身为博客,怎么能只是简单的解决问题? 再次进行深入探索

先放一段代码吧

  1. @ResponseBody
  2. @RequestMapping(value = "test1",produces = "application/xml;charset=utf-8")
  3. public UserInfoInRun test1(){
  4. return new UserInfoInRun();
  5. }
  6. @ResponseBody
  7. @RequestMapping(value = "test2", produces = "application/json;charset=utf-8")
  8. public UserInfoInRun test2(){
  9. return new UserInfoInRun();
  10. }
  11. @ResponseBody
  12. @RequestMapping(value = "test3", produces = "text/html;charset=utf-8")
  13. public UserInfoInRun test3(){
  14. return new UserInfoInRun();
  15. }

运行结果发现

  • test1 出现此问题
  • test2 以json串的形式输出
  • test3 出现此问题

不难发现,输出的应该是一个类,类可以转换为json串,但是不能进行其他类型的转换,从而导致转换失败,而出现此问题

那么是不是可以多一个排查问题的方式……

那么如果我不加限定会怎么样呢

  1. @ResponseBody
  2. @RequestMapping(value = "test4")
  3. public UserInfoInRun test4(){
  4. return new UserInfoInRun();
  5. }
  6. @ResponseBody
  7. @RequestMapping(value = "test5")
  8. public String test5(){
  9. return "test";
  10. }
  11. @ResponseBody
  12. @RequestMapping(value = "test6")
  13. public int test6(){
  14. return 999;
  15. }
  16. @ResponseBody
  17. @RequestMapping(value = "test7")
  18. public int[] test7(){
  19. int[] arr = {2,3,4};
  20. return arr;
  21. }
  • test4 输出正常
  • test5 输出正常
  • test6 输出正常
  • test7 输出正常

有此可见,如果不添加相应限定,SpringMVC会自动的进行转换,当然对于程序来说可能会没有那么严谨,毕竟规则的制定跟程序的安全还是相关的

发表评论

表情:
评论列表 (有 0 条评论,384人围观)

还没有评论,来说两句吧...

相关阅读