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
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With