Java Spring: Passing a HTTP Request to another webapp and get the response back?

1.3k views Asked by At

What i want to do is basically pass a HTTP request from one of my spring controllers to another controller which is inside a another webapp running on another server, and get the response back.

Example of the controller which should do this.

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(HttpServletRequest httpServletRequest) {
     String result="";
     //pass httpServletRequest to another webapp and set the response to result
    return result;

}

One way i could do this is by reading the request headers,request body etc. and make another http post request to that server using something like http components.

But was wondering if there is a much simpler way of doing this(e.g. perhaps just pass the entire httpServletRequest object in one go to the other webapp)?

Thanks.

1

There are 1 answers

0
Josef Prochazka On

it depends on what you want to do. If you need to redirect the user view to the other controller/app you can simply return

result = "redirect:" + yourredirecturl;

or if you want to work with returned html or json ...

You can use Spring restTemplate

class ... {
private RestTemplate restTemplate = new RestTemplate();

String getUrl(Url url)
return restTemplate.getForObject(URI url, String.class) ;

}