繁体   English   中英

一起使用@RequestBody和@ModelAttribute?

[英]Using @RequestBody and @ModelAttribute together?

我正试图进入POST的主体,我希望我的方法的参数绑定到一个对象。

这可能吗?

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

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

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

- 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

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

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

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

警告:以下答案适用于在同一处理程序方法中需要@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"
}

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

不幸的是,这是不可能的。 如果你正在使用Spring MVC的portlet版本(它看起来像是从日志中),那么你可能会对这个JIRA问题感兴趣。

AnnotationMethodHandlerAdapter在内部使用PortletHandlerMethodInvoker,第二个是HandlerMethodInvoker的内部子类 - 您可以在其中配置HttpMessageConverter-s。 但他们被设置为null。 该物业是最终的。

如果你可以替换HandlerMethodInvoker,那甚至可以解决方法,但你不能......它是构造函数创建的;)

需要注意的一点是,Spring MVC的Servlet版本完全支持HttpMessageConverter-s,并且不会遇到这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM