简体   繁体   English

我可以使用web.xml设置Jersey客户端的配置吗

[英]Can I set configuration of Jersey Client with web.xml

In my application I have config classes for Jersey Server and for Jersey Client. 在我的应用程序中,我有Jersey服务器和Jersey客户端的配置类。
Configuration for server is the class extended from ResourceConfig, and I apply it with web.xml 服务器的配置是ResourceConfig扩展的类,我将其与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 客户端的配置是从ClientConfig扩展的类,我在客户端创建期间将其应用

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). 我想要做的就是将此JerseyClientConfig用作所有已创建客户端的默认配置(不想在每次构建客户端距离时都创建config实例)。
Is it possible to set it in web.xml, like I do it to server config? 是否可以像我对服务器配置那样在web.xml中进行设置?
I didn't find anything about it in official documentation. 我没有在官方文档中找到任何有关它的信息。

You can do so for injected Client / WebTarget , take a look at Managed JAX-RS Client article to find out how. 您可以对注入的Client / WebTarget ,请WebTarget Managed JAX-RS Client文章以了解如何进行。 Injected WebTarget looks like: 注入的WebTarget看起来像:

@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): 否则,如果您想自己创建客户端实例,则只能使用JAX-RS应用程序的配置(因此没有真正的自定义配置):

@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. 注意:在此用例中,客户端将具有服务器端具有的所有适用配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM