Why not just expose the REST service for each type of HTTP request and forward it based on the information in the path? For example (untested probably will not work as is, but you get the main idea):
@Autowired RestTemplate restTemplate; @Value("${rest.proxy.target.base.url}") String targetBaseUrl; @RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.GET) public @ResponseBody String restProxyGet(@PathVariable("restUrlPath") String restUrlPath) { return restTemplate.getForObject(targetBaseUrl+ "/" + restUrlPath, String.class); } @RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.POST) public @ResponseBody String restProxyPost(@PathVariable("restUrlPath") String restUrlPath, @RequestBody String body) { return restTemplate.postForObject(targetBaseUrl + "/" + restUrlPath, body, String.class); } //Can also add additional methods for PUT, DELETE, etc. if desired
If you need to communicate with different hosts, you can simply add another path variable that acts as the key to the map, which stores different destination base URLs. You can add any authentication you want from the controller, or using special authentication in Spring Security.
Your question is somewhat covered in detail, so your specific scenario may complicate the situation, but the basic approach should work.
source share