@RequestBody or @ModelAttribute with Spring+REST web services

后端 未结 2 1408
遥遥无期
遥遥无期 2020-12-08 17:33

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 (

相关标签:
2条回答
  • 2020-12-08 17:52

    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.

    0 讨论(0)
  • 2020-12-08 18:08

    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>
    
    0 讨论(0)
提交回复
热议问题