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.
Looks like you’re using the wrong annotation.
@RequestBodyis 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.