Spring 3.2 forward request with a new object

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

Below is the exact scenario:

Twilio calls the controller method with the data sent by the client, as follows:

@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 redirect this request to an ActivityController, which is already processing a request from network / leisure clients. ActivityController.java has a method with the following signature:

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

}

Is it possible? If so, how?

Thanks,

0
source share
1 answer

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

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

Source: https://habr.com/ru/post/1017094/


All Articles