Convert an @RequestBody object to an object

Guys, well, I have done enough research, but I can’t find a solution to this.

In short, I just pass url encoded data to the Controller method and try to convert it as a domain object that has a date and integers.

@RequestMapping(value = "/savePassport", method = RequestMethod.POST) public @ResponseBody AjaxResponse savePassport(@RequestBody StaffPassport passport, HttpServletResponse response) { // Some operations. 

}

The personnel passport is as follows:

 import java.sql.Date; public class StaffPassport { private int staffId; private String passportNumber; private String placeOfIssue; private Date issueDate; private Date expiryDate; private String spouseName; private String oldPassportRef; private String visaInfo; private String description; //gets/sets } 

When I call / savePassport, I get an unsupported media exception. I assume this is due to casting.

I cannot work correctly. Of course, I can catch individual form data using @RequestParam and manually cast, but that’s not the point of the frame, is it?

Where am I mistaken? And you are right. I'm new to Spring, but I like it.

+7
source share
6 answers

It looks like you are using the wrong annotation. @RequestBody designed to request a request that has arbitrary content in its body, such as JSON, certain XML application code, comma-separated variables .. whatever. And using the marshaller that you configure in the dispatcher servlet to turn it into objects.

If all you want to do is ask Spring to bind the old old form to the support object for you, the correct annotation is to set the @ModelAttribute method @ModelAttribute .

+9
source

If you are sending a JSON object using jQuery and you want Spring to be able to handle it using @RequestBody, use JSON.stringify (....) in your data. Here is an example:

 var data = { "id": 3, "name": "test" } $.post("processJsonData.html",JSON.stringify(data), function(data){ ... } ); 

If you are not using JSON.stringify (), you will send the data as form data, and Spring will tell you that you have an unsupported media type.

+1
source

First of all, make sure that you have

 <mvc:annotation-driven /> 

in the Spring configuration file. This is necessary for working with JSOn in Spring MVC.

Secondly, I recommend that you test the user request so that the application type / json is on the server. I believe Fiddler2 will help you do this.

Third, but I'm not sure about this, try changing the Date elements in your POJO from SQL type to regular java type.

UPDATE: just looked at the form and it seems that your “Accept” HTTP Header should also be application / json. Please check this question on Fiddler2.

0
source

I assume you are posting JSON and want Spring to convert to StaffPassport. If you get an Unsupported media exception, it is because Spring could not find a suitable way to perform the conversion.

Spring needs Jackson to convert JSON - make sure your project has Jackson banks. If this is a Maven based project, you can add the jackson-mapper-asl artifact id to your pom.xml. This should give you json-mapper and json-core jars.

Edit: I have to mention that this applies to Spring 3 (I recently ran into this problem). I'm not sure what else is needed for previous versions of Spring.

0
source

Check the HttpMessageConverter interface and its implementation. You can write your own implementation to convert it to the required domain model. By the time the control receives your method, you can access it as if the domain model object was passed.

0
source

Well, I think I should clarify my answer. I have no direct experience using it in a spring-mvc project, but spring integration. I am sure that the applicable media type (application / x-url-form-encoded) has already been processed and converted to MultiMap using the Spring framework; therefore, extract the values ​​from this, like any other card with the key value being your form variable, and fill out your business model.

NTN.

-one
source

All Articles