1. 程式人生 > >Spring Boot: HttpMediaTypeNotAcceptableException: Could not find acceptable representation原因及解決方法

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

錯誤場景

使用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會直接使用內建的處理靜態資源的控制器返回靜態檔案而不再去查詢使用者定義的控制器、

相關推薦

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

錯誤場景 使用Spring Boot的Web專案,在其 resources/static/目錄下存在login.html靜態檔案,同時還有一個處理/login請求的控制器方法(該方法會返回JSON格式

sping boot 報:Could not find acceptable representation原因解決方法

但是我這個是因為用了swagger ui 測試資料的時候返回時 選擇了xml格式導致報了Could not find acceptable representation 把返回資料格式換成 就可以了,這是我個人的錯誤場景,有朋友相同的可以嘗試修改下

SpringBoot踩坑指南(三):Could not find acceptable representation原因解決方法

Spring Boot:Could not find acceptable representation原因方法名跟頁面、js重合了!!!解決辦法:改名字,儘量不要重合。例子:① html的名字   s

SpringBoot報406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

      SpringBoot菜雞最近使用@ResponseBody返回一個map集合,集合包含資料總條數及資料集合list,方法如圖:       結果執行時斷點進了後臺但是後臺報錯,異常資訊為: WARN (AbstractHandlerExceptionRes

Could not find acceptable representation

json https param tor default dia iteye logs tps 報了一個這個異常: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acc

Springmvc 406 狀態碼 / Could not find acceptable representation

查閱資料,大都表示需要 加入依賴的jar,jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar問題解決。 經測試不是這個錯誤不是必須依賴

SpringMVC專案新增@ResponseBody無效,Could not find acceptable representation

配置: spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sprin

Could not find acceptable representation錯誤

nta not find post rod request repr 出發 utf-8 發現 Could not find acceptable representation:“找不到可接受的形式” 發現可能出發這種異常情況的原因: 請求co

Xcode8不能在iOS7上除錯,Could not find Developer Disk Image的解決方法

升級完Xcode8之後,預設情況下,是無法進行iOS7的除錯的,因為Xcode8裡面不再遊iOS7的映象檔案,所以會提示 Could not find Developer Disk Image, 解決

Linux環境下gcc靜態編譯/usr/bin/ld: cannot find -lc錯誤原因解決方法 原因

原因: 一般出現這個問題的時候,Makefile中肯定有-static選項。這其實是靜態連結時沒有找到libc.a。 解決方案: 需要安裝glibc-static.xxx.rpm,如glibc-static-2.12-1.107.el6_4.2.i686.rpm,或是yum install gli

mac上 go-delve 安裝出現The specified item could not be found in the keychain 解決方法

mon pen 方法 rar spec hub 錯誤 sys lib 安裝go語言的編譯環境,在安裝到 delve時候出現如下錯誤: If reporting this issue please do so at (not Homebrew/brew or Hom

Dockerfile中npm中Error: could not get uid/gid問題的解決方法

dockerfile 中  使用 npm 的時候報錯:   解決辦法: https://github.com/tootsuite/mastodon/issues/802     &nbs

spring boot maven 打包提示 “程式包找不到” 解決方法

再用springboot maven打包的時候會碰到 找不到“程式包找不到” 的問題。解決放下如下 修改父pom.xml配置檔案。 由原來的 <build> <plugins> <plugin>

Spring Boot下 org.hibernate.AnnotationException異常的一種解決方法

異常如下 Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.xiaomo.xxxx.model.yyy.Clock.user references an un

win7下設定jar包關聯方法/could not create the java virtual machine 解決方法

首先 右鍵 .jar 字尾檔案 - 開啟方式 - 選擇預設程式 - 瀏覽 - 找到你的jdk安裝目錄(我的是D:\Java\jdk1.7.0_04\) - 找到bin資料夾 - 選中javaw.exe 然後點開啟 但是注意 此時仍是無法執行jar包的 還需要 按Win + R 輸入rege

造成javax.servlet.ServletException: Could not resolve view with name的各種原因解決方法

報錯完成資訊: javax.servlet.ServletException: Could not resolve view with name '/lose/index' in servlet with name 'SpringMVC' 注:藍色部分根據你的實際情況

loadrunner上傳檔案後,報“Error -26488: Could not obtain information about submitted file”解決方法

問題:·loadrunner 上傳檔案回放錯誤 回放時lr報錯:Error -26488: Could not obtain informationabout submitted file 解決辦法:錄製完指令碼後,把要上傳的檔案放到指令碼存放的資料夾裡面,重新回放就ok

Spring boot 整合CXF webservice遇到的一些問題解決

在spring boot整合CXF開發是遇到的一些問題    以及整合方式整合過程    網上資料引入cxf-spring-boot-starter-jaxws依賴即可<dependency> <groupId>org.apache.cxf&l

Centos上搭建jenkins,並自動部署Spring Boot專案。(附遇到的問題解決方案)

最近嘗試自己搭建jenkins自動部署環境,將整個過程及遇到的問題記下來,希望能夠幫助到一些朋友! 本文使用的環境是:Cent os7.2,maven3.5.3,JDK8,jenkins2.136 首先需要在你的伺服器上配好相關的JDK與maven,還要在系統上安裝好gi

Could not build module 'XCTest'錯誤的解決方法

這是在xcode 8.3 上面在模擬器上面試圖執行單元測試時遇到的詭異錯誤。不僅在已有工程上面出現,甚至新建立一個帶單元測試target的工程,也會如此。 它不影響單純build的成功,不影響單純run的成功。只有當單元測試在模擬器上執行時,它才會出現,它將導