简体   繁体   English

如何将HttpServletResponse传播到客户端?

[英]How to propagate an HttpServletResponse to a client?

We're working on a new web application to replace an old one that is commonly being used by our company. 我们正在开发一个新的Web应用程序来替换我们公司通常使用的旧应用程序。 They both will ultimately have the exact same API. 它们最终都将拥有完全相同的API。 What we'd like to do initially is test out the new functionality by having clients send requests to the new web app, but then have our new web app propagate their requests to the old web app and send the old web app's http responses back to our clients (so that from client point of view, nothing has changed). 我们最初想要做的是通过让客户端向新的Web应用程序发送请求来测试新功能,然后让我们的新Web应用程序将他们的请求传播到旧的Web应用程序并将旧的Web应用程序的http响应发送回我们的客户(从客户的角度来看,没有任何改变)。

What I'd like to do is get the exact HttpServletResponse object that we're getting back from the old web app and send that back to clients of the new web app. 我想要做的是获取我们从旧的Web应用程序返回的确切的HttpServletResponse对象,并将其发送回新的Web应用程序的客户端。 What is the best way of performing this? 执行此操作的最佳方式是什么? I know that once I can retrieve the HttpServletResponse, I can just set it equal to the one we have as a parameter to our functions (API handlers) in our new web app's REST controller, but I'm having trouble retrieving it. 我知道,一旦我可以检索HttpServletResponse,我就可以将它设置为我们在新的Web应用程序的REST控制器中作为函数(API处理程序)的参数,但是我在检索它时遇到了问题。

Is there any way of retrieving the HttpServlet response via Spring's RestTemplate? 有没有办法通过Spring的RestTemplate检索HttpServlet响应?

You can use org.apache.commons.httpclient.HttpClient 您可以使用org.apache.commons.httpclient.HttpClient

It has public int executeMethod(HttpMethod method) where you can define request and after checking status is success 它有public int executeMethod(HttpMethod method) ,您可以在其中定义请求并在检查状态后成功

you can use all the HttpMethod methods. 你可以使用所有的HttpMethod方法。

InputStream getResponseBodyAsStream()
Header[] getResponseHeaders();

and pass to your response. 并转到你的回复。 Like this 像这样

public static void main(String[] args) throws Exception {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod("http://The old service url");
    method.addRequestHeader("X-Username", "user");
    method.addRequestHeader("X-Password", "password");
    // Execute the HTTP GET request
    int status = client.executeMethod(method);
    if(status == 200){
        InputStream in = method.getResponseBodyAsStream();
        OutputStream out=the out of your response
        IOUtils.copy(in, out);
        in.close();
        out.flush();
        out.close();
    }
}

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

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