Using JAX-RS (RESTEasy) as middleware: brokering a client's request to another server

强颜欢笑 提交于 2020-01-15 15:13:10

问题


I'm building a server using JAX-RS (RESTEasy) that will provide a REST interface to clients. The server will act as a broker between the client and another server. The other server, a 3rd-party server (JasperReports), also has a REST interface. I'd like to use JAX-RS to have my broker talk to that server. (My broker server adds authentication and other services.) So, there you have the three parties: client, broker-server, reports-server.

I see the workflow this way. The broker-server implements JAX-RS (server) to get the client's requests, repackage them, and pass them along to the reports-server, using JAX-RS (client). When the broker-server has obtained a report, I'd like to relay that back to the client. But, so far, I believe that's where things break down.

Here's some code:

// Server gets a request and passes it to its (internal) client, handler.
@GET
@Path("/jobs")
public Response fetchAllScheduledJobs() {
    ReportScheduleHandler handler = new ReportScheduleHandler();
    Response response = handler.fetchAllScheduledJobs();
    return response;
}

Here is the handler sending that off to the reports-server...

public Response fetchAllScheduledJobs() {
    Client client = ClientBuilder.newClient();

    client.register(getBasicAuthentication());
    Response response = 
        client.target(getReportsBaseUri())
            .request()
            .accept("application/json")
            .get();

    client.close();
    return response;
}

So, in my (misguided) thinking, I'm thinking that the broker-server just returns the response back to the client, and all is well. But, as I said above, the client is getting nothing back. I'm using a REST developer's client ("Postman"), and here are the headers I'm getting back:

Cache-Control →private
Content-Length →0
Content-Type →application/json
Date →Mon, 14 Jul 2014 16:05:46 GMT
Expires →Wed, 31 Dec 1969 19:00:00 EST
P3P →CP="ALL"
Server →Apache-Coyote/1.1
Transfer-Encoding →chunked

(Copied and pasted, it looks just like that. I have no idea why Postman shows these arrows!)

Any idea what I'm missing here? Does the broker need to somehow unpack the Response it receives from its internal client and repackage that before returning it to the original client? Please ask any questions you need for clarification. Thanks!

Edit

Wait! Could it be that my Response has an input stream and that I need to read that and write out and output stream to the client -- or something like that?


回答1:


You're closing your client, therefore not unwrapping the Response in an open client context. Unwrap your response, close the client, and return your unwrapped object.

edit:

Sorry, not your client. I believe in the Response object you've got a close() method.

Pretty much like this:

Client client = ClientFactory.newClient();
            WebTarget target = client.target("http://foo.com/resource");
            Response response = target.request().get();
            String value = response.readEntity(String.class);
            response.close();  // You should close connections!
return value;


来源:https://stackoverflow.com/questions/24742305/using-jax-rs-resteasy-as-middleware-brokering-a-clients-request-to-another-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!