@RequestBody or @ModelAttribute with Spring+REST w

2020-02-03 05:18发布

问题:

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

回答1:

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.



回答2:

Its @RequestBody. I feel its better to specify the mime type that you are expecting and producing as output using @RequestMapping as,

  @RequestMapping(value="/authenticate",produces="application/json",   
consumes="application/json",method=RequestMethod.POST)

Then register appropriate message converters with AnnotationMethodHandlerAdapter

This message converter is responsible for Marshalling & unmarshalling of your request & response entity based on produces & consumes attributes.

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
     <property name="order" value="1" />
         <property name="messageConverters">
         <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >

                 <property name="supportedMediaTypes" value="application/json"/>
            </bean>
            <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
            </bean>
         </list>
    </property>
</bean>