@RequestBody does not work. Return Status Http. 415 unsupported media type in jQuery

I am using Spring 3.1 to develop my project. During work I got stuck at a certain point and really need your help.

My request from the client I will receive a JSON object and return its JSON object. I successfully implement the same when I use sending and deleting a request from the server. But when I use the PUT method to send my data encountering some kind of problem. Since PUT cannot receive data in @ModelAttribute , I use the @RequestBody annotation to send my data from the client.

When I use the body of @RequestBody MultiValueMap<String, String> you get an error

Http 415 status of unsupported media.

When I try to get data using @RequestBody DemandBean (my Bean project), I get the following error.

org.codehaus.jackson.JsonParseException: Unexpected character ('o' (code 111)): expected valid value (number, string, array, object, true, false or zero) in [Source: org.apache.catalina.connector.CoyoteInputStream@19 d688; row: 1, column: 2]

But I am absolutely sure that I correctly matched my Jackson library, because with @RequestBody I can return json back to the client, and also send Json and Spring can parse with @ModelAttribute in case of the GET POST , DELETE method.

Below I give the code:

Html FIle to send data:

  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 format is sent to the server:

 [{"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> 

Controller Class:

 @RequestMapping(method = RequestMethod.PUT) public @ResponseBody List<DemandBean> searchDemandDetailsWithPut(@RequestBody DemandBean demand, HttpServletResponse response) throws IOException { } 
+4
source share
2 answers

Try changing the json that you send from the array of objects to an object containing other objects, for example:

 {{"name":"opportunityId","value":"ad"},{"name":"dem andId","value":"hgh"},{"name":"demandCreator","val ue":"hghhgh"},{"name":"demandOwner","value":"hg"}} 

Instead

 [{"name":"opportunityId","value":"ad"},{"name":"dem andId","value":"hgh"},{"name":"demandCreator","val ue":"hghhgh"},{"name":"demandOwner","value":"hg"}] 
+1
source

I had a similar problem, and in my case, the problem was not in the ajax message: since I connected the ajax call with the click of the form button, after the ajax request was sent, and this caused an error, Preventing form submission resolved the problem:

 $('#btn_confirm').click(function (e) { e.preventDefault(); // do not submit the form // your ajax call here } 
0
source

Source: https://habr.com/ru/post/1395880/


All Articles