Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestBody or @ModelAttribute with Spring+REST web services

I am creating a Restful website and Web services for iPhone and android apps with Spring 3.1. In my application, i am using Spring Message Convertors (org.springframework.http.converter.json.MappingJacksonHttpMessageConverter) to converting JSON into Java object and vice-versa.

My objective is that there should be only single controller method(same URL) that should be used by JSP page, Iphone/Andois app.

I am using Spring form tag for object binding from JSP to controller with the help of @ModelAttribute like below.

@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@ModelAttributeForgot forgotPassword,
     HttpServletRequest request) { 

     System.out.println("data recived=="+forgotPassword.getNewPassword());
 }

But the same is NOT working in the case if the data is being posted from iPhone/Android app and the result is:

data recived==null;

So to overcome this problem i have used @RequestBody annotation at place of @ModelAttribute.

So my controller looks like below:

@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@RequestBody Forgot forgotPassword,
    HttpServletRequest request) { 

    System.out.println("data recived=="+forgotPassword.getNewPassword());
}

It works then and the result i got is:

data recived==somedata;

But @RequestBody then doesn't work with spring form on JSP page and the data doesn't get converted into object and i got null values.

  1. Can't i use @RequestBody annotation to post data in form of JSON with spring form tag from JSP page??
  2. Is there any way by using which i can post data from my JSP form as well as from I phone App by using only a single controller method(either @ModelAttribute or @RequestBody).

EDIT:

While writing String in place of Bean class, i am able to get the content in form of plain text, as below:

@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@RequestBody String string,
     HttpServletRequest request) { }

Result from web page call:

uid=11&confirmPassword=somepassword&newPassword=somepassword

Result from iPhone using web service call(in **JSON)**

{"newPassword":"somepassword","confirmPassword":"somepassword","uid":"11"}

But problem is that using this approach i have to parse the JSON string into Java object manually. And in web page content i have to find the values manually that i don't want.

Please help.

Regards,

Arun Kumar

like image 987
Arun Kumar Avatar asked Nov 05 '12 09:11

Arun Kumar


1 Answers

Sorry, but I don't believe there is a way, because @ModelAttribute is bound from form post parameters and @RequestBody passes the body straight to the Json converter. You could replace the spring form tag with a simple json post, but that is probably less convenient than having two @RequestMapping methods.

like image 79
Kevin Schmidt Avatar answered Oct 11 '22 16:10

Kevin Schmidt