我已經看過所有其他答案這個問題,並嘗試了大多數的解決方案,但似乎沒有任何工作。我使用的是Dojo.xhrPOST(xhrArgs),它顯然是在xhrArgs定義之後出現的。Spring MVC控制器拋出415錯誤媒體不支持JSON請求
我xhrArgs:
xhrArgs =
{
headers:
{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
'url': thisUrl,
'postData':requestString,
'dataType' : 'json',
'userId': userId,
'measurementSystem': measurementSystem,
'systemId': openedSystemId,
'handleAs': 'text',
'load': function(data)
{
// Replace newlines with nice HTML tags.
data = data.replace(/\n/g, "<br/>");
dojo.byId(target).innerHTML = data;
},
'error': function(error)
{
dojo.byId(target).innerHTML = error;
}
};
和我的控制器方法簽名和註解如下
@RequestMapping(value="/saveSystemConditions", method= RequestMethod.POST, headers = {"content-type=application/json"})
public String saveSystemConditions(HttpServletRequest request, HttpServletResponse response, @Valid @RequestBody Load load, BindingResult result)
和我requestString如圖所示xhrArgs是
"{'systemID':'76', 'system.systemType':'1', 'unitsOfMeasure':'english', 'loadID':'63', 'dispersionInstallationLocation':'Duct+or+AHU', 'humidificationSystemType':'1', 'totalAirVolume':'1200.0', 'desiredDryBulb':'70.0', 'desiredAirMoistureType':'2', 'desiredAirMoisture':'55.0', 'outsideAirConditionsType':'1', 'outsideAirIntakeRateMeasuredAs':'0', 'loadCountry':'United+States', 'outsideAirVolumeMeasuredIn':'0', 'loadState':'Minnesota', 'outsideAirIntakeRate':'25.0', 'loadCity':'Minneapolis', 'elevationFeet':'837.0', 'outsideDryBulb':'-6.8', 'outsideAirMoisture':'57.3', 'userEnteredLoad':'7.43'}"
我得到 415(不支持的介質類型)
任何建議將不勝感激。 David
你能分享你的MVC配置嗎?你的classpath中有jackson庫嗎?如果你正在返回一個字符串值,你可以刪除「Accept」:'application/json''頭並嘗試。這裏的問題看起來像應用程序不知道如何將返回值'String'轉換爲'json'對象。如果你在classpath有傑克遜庫很會照顧這個 –
我在pom.xml中有這樣的 \t org.codehaus.jackson 傑克遜核心 - 翔升 \t 1.7.5 \t 編譯 org.codehaus.jackson \t jackso正映射-ASL 1.7.5 編譯 –
我更新到1.9.11和更新我的POM,以反映該版本。仍然獲得415不支持的媒體類型。 –