Firdous Amir
Firdous Amir

Reputation: 1303

Converting @RequestBody to an object

Guys, Well I have done enough research still I can't find the solution to this.

In a nutshell, I'm simply passing url encoded form data to the Controller method and trying to convert it as a domain object which has Date and integers.

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

    // Some operations.

}

The Staff Passport looks like this:

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 invoke the /savePassport, I get unsupported media exception. I guess it's related to casting.

I can't this working right. Of course I can catch individual form data using @RequestParam and manually do the casting but that's not the point of a framework isn't it?

Where am I going wrong? And you are right. I'm a beginner in Spring, but I love it.

Upvotes: 8

Views: 14740

Answers (6)

Flyhard
Flyhard

Reputation: 554

If you are posting a JSON Object with jQuery and you want Spring to be able to process it with @RequestBody, use JSON.stringify(....) in your data. Here an example:

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

If you don't use the JSON.stringify() then you will submit the data as form data and Spring will tell you that you have an unsupported media type.

Upvotes: 1

Bobby Fisher
Bobby Fisher

Reputation: 92

Ok, I think I should refine my answer. I do not have direct experience of using it in a spring-mvc project but spring-integration. I am pretty sure the applicable media type (application/x-url-form-encoded) is already handled and converted to MultiMap by Spring framework; so, retrieve the values from that just like any other map with the key value being your form variable and populate your business model.

HTH.

Upvotes: -1

Bobby Fisher
Bobby Fisher

Reputation: 92

Check into HttpMessageConverter interface and its implementations. You could write your own implementation of it to convert it to the domain model you want. By the time the control gets to your method, you can access it as if your domain model object is passed.

Upvotes: 0

David
David

Reputation: 1521

I assume that you are posting JSON and want Spring to convert to StaffPassport. If you are getting an Unsupported media exception, it is because Spring could not figure out an appropriate way to perform the conversion.

For Spring to convert JSON, it needs Jackson -- make sure you have the Jackson jars in your project. If this is a Maven based project you can add the jackson-mapper-asl artifact ID to your pom.xml. This should give you the jackson-mapper and jackson-core jars.

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

Upvotes: 0

Affe
Affe

Reputation: 47994

Looks like you're using the wrong annotation. @RequestBody is for taking a request that has arbitrary content in its body,such as JSON, some application defined XML, comma separated variables.. whatever. And using a marshaller that you configure in the dispatcher servlet to turn it into objects.

If all you want to do is ask Spring to bind a plain old form post onto the backing object for you, the correct annotation to put on the method parameter is @ModelAttribute.

Upvotes: 9

danny.lesnik
danny.lesnik

Reputation: 18639

First of all be sure that you have

<mvc:annotation-driven /> 

in your Spring configuration file. This is mandatory for working with JSOn in SPring MVC.

Second, I recommend you to test wether request to the server has application/json content type. I belive Fiddler2 will help you to do so.

Third, but I'm not sure about it, Try to change Date items in your POJO from SQL type to regular java type.

UPDATE: just looked at the Form and it seems like your "Accept" HTTP Header should be also application/json. Please test this issue with Fiddler2 as well.

Upvotes: 0

Related Questions