我正在使用Spring 3.1開發我的項目。在工作中,我堅持一點,真的需要你的幫助。@RequestBody不工作 - 返回Http狀態415 Jquery中不支持的媒體類型
我的要求是從客戶端我會收到JSON對象,也會返回JSON對象。當我使用從服務器獲取發佈和刪除請求發送時,我成功實現了同樣的功能。但是當我使用PUT
方法發送我的數據時遇到了一些問題。因爲PUT
不能接收@ModelAttribute
中的數據我使用@RequestBody
註釋來接收從客戶端發送的數據。
當我使用@RequestBody MultiValueMap<String, String>
的身體得到一個錯誤
Http狀態415不支持的媒體類型。
當我嘗試用@RequestBody DemandBean
(我的項目的Bean)我收到以下錯誤接收數據。
org.codehaus.jackson.JsonParseException:意外字符( 'O'(代碼111)):預測的有效值(數字,字符串,數組,對象, '真', '假' 或 '空' ) at [Source:[email protected] d688;線:1,列:2]
但我敢肯定,我已經映射到我的傑克遜庫正確,因爲與@RequestBody
我可以接收JSON回客戶端,也可以發送JSON和彈簧可以@ModelAttribute
在解析方法的情況是GET
,POST
,DELETE
。
下面我給的代碼:
HTML文件發送數據:
var jsonStr = $("#searchDemand_frm").serializeArray();
$("#searchResultTable td").remove();
alert(JSON.stringify(jsonStr)); // Return proper form data in json format
$.ajax({
contentType : "application/json",
dataType : 'json',
type : "PUT",
url : targetUrl,
data : jsonStr,
async : false,
success : function(data) {
alert("In Success");
},
error : function(request, status, error) {
showPermissionDenied(request);
}
});
JSON格式發送到服務器:
[{"name":"opportunityId","value":"ad"},{"name":"dem andId","value":"hgh"},{"name":"demandCreator","val ue":"hghhgh"},{"name":"demandOwner","value":"hg"}, {"name":"status","value":"IP"},{"name":"region","v alue":"hgh"}]
-servlet.xml後綴:
<mvc:annotation-driven />
<context:component-scan base-package="com.ericsson.rms.controller.*" />
<context:component-scan base-package="com.ericsson.rms.application.authorizatio n" />
<context:annotation-config/>
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
控制器類:
@RequestMapping(method = RequestMethod.PUT)
public @ResponseBody
List<DemandBean> searchDemandDetailsWithPut(@RequestBody DemandBean demand,
HttpServletResponse response) throws IOException {
}