Spring 3.2 forward request with new object

2019-09-17 09:57发布

I'm trying to forward a request from one controller to another controller and set a object in request so that the forwarded controller can use it in @RequestBody.

Following is the exact scenario:

Twilio calls a controller method with data sent by client as following:

@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";
}

Now, I want to forward this request to ActivityController which already handles the request from web/rest clients. ActivityController.java has a method with following signature:

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

}

Is it possible? If yes, how?

Thanks,

1条回答
▲ chillily
2楼-- · 2019-09-17 10:26

Create the object and add it to the request as an attribute in the first controller,

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

In the updateLocation Method retrieve that object from the request

@RequestMapping(value = "/activity/new", method = RequestMethod.POST)
public ResponseEntity<Activity> updateLocation(
    @RequestBody Activity activity, HttpServletRequest request) {
   OwnObject o = (OwnObject) request.getAttribute("object");
}
查看更多
登录 后发表回答