Forwarding a response from another server using JA

2019-07-19 08:14发布

I have an angular client which is making a POST call to my server. This server needs to get a response by calling another server(server2) with a POST call and pass the response from the server2 to the client. I tried the following approaches.

public Response call(){
   String server2Url = "http://server2/path"
   RestClient restClient = new RestClient();
   return Response.fromResponse(restClient.post(server2Url)).build();
}

But in the above case the HTTP status code gets transferred but not the response body. The response body is empty

Then I tried:

public Response call() throws URISyntaxException{
   String server2Url = "http://server2/path"
   RestClient restClient = new RestClient();
   return Response.temporaryRedirect(new URI(server2Url)).build();
}

but the browser client ends up making an OPTIONS call to the server2Url instead of a POST

and I tried.

public Response call() throws URISyntaxException{
    String server2Url = "http://server2/path"
    RestClient restClient = new RestClient();
    return Response.seeOther(new URI(server2Url)).build();
}

but this ends up making a GET call instead of a POST.

How do I make the browser client make a POST call to server2

标签: rest http jax-rs
2条回答
Anthone
2楼-- · 2019-07-19 09:15

You can use Html Client from JAX-RS to make your own requests (from server1 to server2) and then return the response from server2 to the angular client.

public Response call() {
    String url = "server2 url";
    Response response;
    try {
        response = ClientBuilder
                .newClient()
                .target(url)
                .request()
                .post(Entity.json(null), Response.class);

    }
    catch (Exception e) {
        // Whatever you want
        return null; // or error
    }

    // Return the status returned by server 2
    return Response.status(response.getStatus()).build();
}
查看更多
倾城 Initia
3楼-- · 2019-07-19 09:15

What you are trying to accomplish is covered in the RFC 2616 I just found here.

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

So it looks like this is out of your hands if you´re not implementing the client.

Edit because I was told that RFC 2616 must not be used any longer.

RFC 7231 states that:

302 Found

The 302 (Found) status code indicates that the target resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client ought to continue to use the effective request URI for future requests.

The server SHOULD generate a Location header field in the response containing a URI reference for the different URI. The user agent MAY use the Location field value for automatic redirection. The server's response payload usually contains a short hypertext note with a hyperlink to the different URI(s).

Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request. If this behavior is undesired, the 307 (Temporary Redirect) status code can be used instead.

What is:

307 Temporary Redirect

The 307 (Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic
redirection to that URI. Since the redirection can change over time, the client ought to continue using the original effective request URI for future requests.

The server SHOULD generate a Location header field in the response containing a URI reference for the different URI. The user agent MAY use the Location field value for automatic redirection. The server's response payload usually contains a short hypertext note with a
hyperlink to the different URI(s).

Note: This status code is similar to 302 (Found), except that it does not allow changing the request method from POST to GET. This specification defines no equivalent counterpart for 301 (Moved Permanently) ([RFC7238], however, defines the status code 308 (Permanent Redirect) for this purpose).

查看更多
登录 后发表回答