spring mvc rest service redirect / forward / proxy [英] spring mvc rest service redirect / forward / proxy
问题描述
我使用spring mvc框架构建了一个Web应用程序来发布REST服务。
例如:
I have build a web application using spring mvc framework to publish REST services. For example:
@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);
}
现在我需要部署我的应用程序,但我有以下内容问题:
客户端无法直接访问应用程序所在的计算机(有防火墙)。因此,我需要代理机器上的重定向层(可由客户端访问)调用实际的休息服务。
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.
我尝试使用RestTemplate进行新的调用:
例如:
I tried making a new call using RestTemplate: For Example:
@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<T>(user, headers), Movie.class);
}
这没关系,但我需要重写每个方法控制器使用resttemplate。此外,这会导致代理计算机上的冗余序列化/反序列化。
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.
我尝试使用restemplate编写泛型函数,但它没有用完:
I tried writing a generic function using restemplate, but it did not work out:
@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(), ? , ?);
}
我找不到一个与请求一起使用的resttemplate方法和响应对象。
I could not find a method of resttemplate which works with request and response objects.
我也尝试过spring重定向和转发。但重定向不会改变请求的客户端IP地址,所以我认为在这种情况下它是无用的。我也无法转发到其他网址。
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.
有没有更合适的方法来实现这一目标?
提前致谢。
Is there a more appropriate way to achieve this? Thanks in advance.
推荐答案
您可以用以下方式镜像/代理所有请求:
You can mirror/proxy all requests with this:
private String server = "localhost";
private int port = 8080;
@RequestMapping("/**")
@ResponseBody
public String mirrorRest(@RequestBody String body, HttpMethod method, HttpServletRequest request) throws URISyntaxException
{
URI uri = new URI("http", null, server, port, request.getRequestURI(), request.getQueryString(), null);
ResponseEntity<String> responseEntity =
restTemplate.exchange(uri, method, new HttpEntity<String>(body), String.class);
return responseEntity.getBody();
}
这不会反映任何标题。
这篇关于spring mvc rest service redirect / forward / proxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!