Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX RS Client API interceptor

Is there a way to add a header into the request via interceptors,but not via explicitly setting a header, when JAX RS Client API is used:

Client client = ClientBuilder.newClient();
Response response = client.target("someUrl").path("somePath").request().get();

In AOP way

like image 328
Alexandr Avatar asked Oct 08 '15 15:10

Alexandr


People also ask

What is JAX-RS Client API?

The JAX-RS client API is a Java based API used to access Web resources. It is not restricted to resources implemented using JAX-RS.

What is interceptor REST API?

Ad. Jakarta Restful Web Services includes an Interceptor API that allows developers to intercept request and response processing. This allows addressing some advanced concepts like authentication, caching, and compressing without polluting application code.

Is JAX-RS client thread safe?

Official JAX-WS answer: No. According to the JAX-WS spec, the client proxies are NOT thread safe. To write portable code, you should treat them as non-thread safe and synchronize access or use a pool of instances or similar.

What is Clientrequestcontext?

Client request filter context. A mutable class that provides request-specific information for the filter, such as request URI, message headers, message entity or request-scoped properties. The exposed setters allow modification of the exposed request-specific information. Since: 2.0 Author: Marek Potociar.


1 Answers

Create a ClientRequestFilter:

@Provider
public class MyClientRequestFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add("Authorization", "value");
    }
}

And register it in your Client:

Client client = ClientBuilder.newClient().register(MyClientRequestFilter.class);
like image 177
cassiomolin Avatar answered Oct 17 '22 04:10

cassiomolin