带有新对象的Spring 3.2转发请求

时间:2014-02-27 16:16:32

标签: spring spring-mvc

我正在尝试将请求从一个控制器转发到另一个控制器并在请求中设置一个对象,以便转发的控制器可以在@RequestBody中使用它。

以下是具体情况:

Twilio使用客户端发送的数据调用控制器方法,如下所示:

@RequestMapping(value = "/sms", method = RequestMethod.POST)
public String receiveSms(@RequestParam("Twiml") String twiml, 
    HttpServletRequest request, 
    HttpServletResponse response) {

    //TODO - Create new instance of Activity and populate it with data sent from client
return "forward:/activity/new";
}

现在,我想将此请求转发给已经处理来自Web / rest客户端的请求的ActivityController。 ActivityController.java有一个带有以下签名的方法:

@RequestMapping(value = "/activity/new", method = RequestMethod.POST)
public ResponseEntity<Activity> updateLocation(
    @RequestBody Activity activity) {

}

有可能吗?如果是,怎么样?

谢谢,

1 个答案:

答案 0 :(得分:1)

创建对象并将其作为第一个控制器中的属性添加到请求中,

request.setAttribute("object",new OwnObject()),
return "forward:/activity/new";

在updateLocation方法中从请求中检索该对象

@RequestMapping(value = "/activity/new", method = RequestMethod.POST)
public ResponseEntity<Activity> updateLocation(
    @RequestBody Activity activity, HttpServletRequest request) {
   OwnObject o = (OwnObject) request.getAttribute("object");
}