繁体   English   中英

Java Spring:将HTTP请求传递到另一个Web应用程序并获得响应吗?

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

我要做的基本上是将一个Spring请求中的一个HTTP请求传递给另一个在另一个服务器上运行的webapp内的另一个控制器,并返回响应。

应该执行此操作的控制器示例。

@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;

}

我可以做到这一点的一种方法是读取请求标头,请求正文等,并使用类似http组件的东西向该服务器发出另一个http post请求。

但是想知道是否有更简单的方法(例如,也许只是将整个httpServletRequest对象一次传递到另一个webapp)?

谢谢。

这取决于您想做什么。 如果您需要将用户视图重定向到其他控制器/应用,则只需返回

result = "redirect:" + yourredirecturl;

或者如果您想使用返回的html或json ...

您可以使用Spring restTemplate

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM