我正在嘗試使用Spring 3.0.6傳回JSON響應,但我得到406響應“Not Acceptable”,其描述如下:
“此請求辨別的資源隻能生成具有特征的響應
根據請求“接受”标題()不可接受.“
我知道之前曾經問過a very similar question,但是我不能讓它為我的項目工作,盡管很多
測試,我不明白我做錯了什麼.
在我的Maven pom.xml中,我有以下内容:
org.codehaus.jackson
jackson-mapper-asl
1.8.5
compile
org.codehaus.jackson
jackson-core-asl
1.8.5
compile
在web.xml中,我引用webmvc-config.xml,日志确認已加載.
mainServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/webmvc-config.xml
1
在webmvc-config.xml中我有以下内容:
我的控制器是:
@Controller
public class ClassifiedController {
@RequestMapping(value = "/classified/{idClassified}", headers = "Accept=**;q=0.8
如果我使用以下JQuery:
$.ajax({
url: '/classified/38001',
type: 'GET',
dataType: 'json'
});
發送以下标頭:
Accept: application/json, text/javascript, */*; q=0.01
在這兩種情況下,結果都是406錯誤.我不知道還應該檢查什麼
這行得通.
更新:我決定通過Spring調試,我發現Jackson被正确調用,并且在org.codehaus.jackson.map.ser.StdSerializerProvider中,方法_findExplicitUntypedSerializer包含以下代碼:
try {
return _createAndCacheUntypedSerializer(runtimeType, property);
} catch (Exception e) {
return null;
}
這是不幸的,因為隐藏了問題的根源.使用調試器,我發現該異常包含一個非常描述性的錯誤消息:
Conflicting getter definitions for property "reminded":
ClassifiedImpl#isReminded(0 params) vs
ClassifiedImpl#getReminded(0 params)
現在,我看到錯誤消息是一個愚蠢的錯誤,很容易修複,但沒有它,這不是那麼明顯.事實上,修複問題,導緻工作序列化.
解決方法:
标簽:java,spring,json,spring-mvc,jackson
來源: https://codeday.me/bug/20190926/1818454.html