user1764832
user1764832

Reputation: 69

Spring 3.2 forward request with new object

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,

Upvotes: 0

Views: 1601

Answers (1)

Eduardo Quintana
Eduardo Quintana

Reputation: 2388

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

Upvotes: 2

Related Questions