一起使用@RequestBody和@ModelAttribute?

Ste*_*all 5 java spring spring-mvc

我试图找到一个主体POST,我想我的方法的参数绑定到一个对象.

这可能吗?

我目前的声明永远不会受到影响:

 @RequestMapping(method = RequestMethod.POST)
public void doStuff(@RequestBody byte[] bodyData, @ModelAttribute Form form, Model model ) {
Run Code Online (Sandbox Code Playgroud)

看起来我得到了这个例外:

- 2011-02-25 16:57:30,354 - ERROR - http-8080-3 - org.springframework.web.portle
t.DispatcherPortlet - Could not complete request
java.lang.UnsupportedOperationException: @RequestBody not supported
Run Code Online (Sandbox Code Playgroud)

thr*_*ups 6

为了使其正常工作,您必须确保使用AnnotationMethodHandlerAdapter.这会覆盖HandlerMethodInvoker的 createHttpInputMessage(抛出您正在看到的异常).(它在私人课堂上这样做.)

我相信你可以在*-servlet.xml中包含以下内容

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
Run Code Online (Sandbox Code Playgroud)

警告:以下答案适用于在同一处理程序方法中需要@RequestBody和@RequestParam的情况.它没有回答这个问题,但可能对某人有用.

我已经使用Spring 3.0.1对此进行了测试.这是可能的,但它有点不稳定.你的@RequestParam参数之前必须有你的@RequestBody方法参数.我猜这是因为HandlerMethodInvoker在检索参数时读取请求体(以及GET参数)(并且请求体只能读取一次).

这是一个例子(警告:我在Scala中编码,所以我没有编译这个Java代码)

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(
        @RequestBody String body,
        @RequestParam("param1") String parma1,
        Map<Object, Object> model: Map[AnyRef, AnyRef])
{
    model.put("test", test)
    model.put("body", body)

    return "Layout"
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用@PathVariable.我已经证实这是有效的.