Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 417
Firdous Amir Avatar asked May 04 '11 21:05

Firdous Amir


People also ask

How to convert JSON Object to Java Object?

A Gson is a json library for java, which is created by Google and it can be used to generate a JSON. By using Gson, we can generate JSON and convert JSON to java objects. We can call the fromJson() method of Gson class to convert a JSON object to Java Object.

Can @RequestBody be a String?

The @RequestBody annotation allows us to retrieve the request's body. We can then return it as a String or deserialize it into a Plain Old Java Object (POJO). Spring has built-in mechanisms for deserializing JSON and XML objects into POJOs, which makes this task a lot easier as well.

Can we use @RequestBody with get in spring boot?

@RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project.


2 Answers

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.

like image 172
Affe Avatar answered Nov 02 '22 17:11

Affe


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.

like image 35
Flyhard Avatar answered Nov 02 '22 18:11

Flyhard