微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax – @RequestBody无法工作 – 返回Http状态415 Jquery中不支持的媒体类型

我使用 Spring 3.1开发我的项目.在工作期间,我坚持了一点,真的需要你的帮助.

我的要求来自客户端,我将收到JSON对象,并将返回JSON对象.当我使用从服务器发送的发送和删除请求时,我成功实现了相同的功能.但是当我使用PUT方法发送我的数据时遇到了一些问题.因为PUT无法在@modelattribute中接收数据,所以我使用@RequestBody注释来接收从客户端发送的数据.

当我使用@RequestBody MultiValueMap< String,String>身体出错

Http Status 415 unsupported media type.

当我尝试使用@RequestBody DemandBean(我的项目Bean)接收数据时,我收到以下错误.

org.codehaus.jackson.JsonParseException: Unexpected character (‘o’ (code 111)): expected a valid value (number,String,array,object,‘true’,‘false’ or ‘null’)
at [Source: org.apache.catalina.connector.CoyoteInputStream@19 d688; line: 1,column: 2]

但我很确定我已经正确映射了我的jackson库,因为使用@RequestBody我可以将json接收回客户端并且还可以发送Json,并且如果方法是GET,POST,DELETE,则spring可以使用@modelattribute进行解析.

下面我给出的代码

Html FIle发送数据:

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 {
}

解决方法

尝试将您从一组对象提交的json更改为包含其他对象的对象,即:

{{"name":"opportunityId","value":"hg"}}

代替

[{"name":"opportunityId","value":"hg"}]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐