Redirecting a response from another server using JAX-RS

I have an angular client that makes a POST call on my server. This server should receive a response by calling another server (server2) with a POST call and transmit the response from server2 to the client. I have 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 is transmitted, 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 the OPTIONS call on server2Url instead of 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 leads to a GET call instead of a POST.

How to make browser client POST call for server2

+1
source share
2 answers

, , RFC 2616 .

302 , GET HEAD, , , , .

, , , .

, , RFC 2616 .

RFC 7231 , :

302

302 () , URI. , URI .

URI URI. Location . URI ().

. POST GET . , 307 ( ) .

:

307

307 ( ) , URI , URI. , URI .

URI URI. Location . URI ().

. 302 (), , POST GET. 301 ( ) ([RFC7238], , 308 ( ) ).

0

Html Client JAX-RS, ( 1 server2), server2 angular.

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();
}
0

Source: https://habr.com/ru/post/1626609/


All Articles