Question

In my application I have config classes for Jersey Server and for Jersey Client.
Configuration for server is the class extended from ResourceConfig, and I apply it with web.xml

<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>blah.blah.core.jersey.config.JerseyServerConfig</param-value>
</init-param>

Configuration for client is the class extended from ClientConfig and I apply it during client creation

Client client = ClientBuilder.newClient(new JerseyClientConfig());

What I want to do is to apply this JerseyClientConfig as default configuration for all created clients (don't want to create instance of config every time client istance is constructed).
Is it possible to set it in web.xml, like I do it to server config?
I didn't find anything about it in official documentation.

Was it helpful?

Solution

You can do so for injected Client/WebTarget, take a look at Managed JAX-RS Client article to find out how. Injected WebTarget looks like:

@Path("resource")
public class MyResource {

    @Uri("http://example.com")
    private WebTarget target;

    @GET
    public String sayHello() {
        return target.request().get(String.class);
    }
}

Otherwise, if you want to create a client instance yourself you can only use configuration of the JAX-RS application (so no really custom configuration):

@Path("resource")
public class MyResource {

    @Context
    private Configuration config;

    @GET
    public String sayHello() {
        return ClientBuilder
                   .newBuilder()
                   .withConfig(config)
                   .target("http://example.com")
                   .request().get(String.class);
    }
}

Note: In this use-case your client will have all the applicable configuration you server-side has.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top