title: spring mvc rest service redirect / forward / proxy
body: <p>I have build a web application using spring mvc framework to publish REST services.
  For example:</p>
  
  <pre><code>@Controller
  @RequestMapping("/movie")
  public class MovieController {
  
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public @ResponseBody Movie getMovie(@PathVariable String id, @RequestBody user) {
  
      return dataProvider.getMovieById(user,id);
  
  }
  </code></pre>
  
  <p>Now I need to deploy my application but I have the following problem:
  The clients do not have direct access to the computer on which the application resides (There is a firewall). Therefore I need a redirection layer on a proxy machine (accessible by the clients) which calls the actual rest service.</p>
  
  <p>I tried making a new call using RestTemplate:
  For Example:</p>
  
  <pre><code>@Controller
  @RequestMapping("/movieProxy")
  public class MovieProxyController {
  
      private String address= "http://xxx.xxx.xxx.xxx:xx/MyApp";
  
      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public @ResponseBody Movie getMovie(@PathVariable String id,@RequestBody user,final HttpServletResponse response,final HttpServletRequest request) {
  
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          RestTemplate restTemplate = new RestTemplate();
          return restTemplate.exchange( address+ request.getPathInfo(), request.getMethod(), new HttpEntity&lt;T&gt;(user, headers), Movie.class);
  
  }
  </code></pre>
  
  <p>This is ok but I need to rewrite each method in the controller to use the resttemplate. Also, this causes redundant serialization/deserialization on the proxy machine. </p>
  
  <p>I tried writing a generic function using restemplate, but it did not work out:</p>
  
  <pre><code>@Controller
  @RequestMapping("/movieProxy")
  public class MovieProxyController {
  
      private String address= "http://xxx.xxx.xxx.xxx:xx/MyApp";
  
      @RequestMapping(value = "/**")
      public ? redirect(final HttpServletResponse response,final HttpServletRequest request) {        
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          RestTemplate restTemplate = new RestTemplate();
          return restTemplate.exchange( address+ request.getPathInfo(), request.getMethod(), ? , ?);
  
  }
  </code></pre>
  
  <p>I could not find a method of resttemplate which works with request and response objects.</p>
  
  <p>I also tried spring redirect and forward. But redirect does not change the request's client ip address so i think it is useless in this case. I could not forward to another URL either.</p>
  
  <p>Is there a more appropriate way to achieve this?
  Thanks in advance.</p>