简体   繁体   English

JAX-RS客户端的自定义主机名解析程序

[英]Custom hostname resolver for JAX-RS client

Is there a way to customize the host name resolution inside a JAX-RS client? 有没有办法在JAX-RS客户端内自定义主机名解析?

I am using javax.ws.rs.client.ClientBuilder to create a client and I would like that for example https://mytestinghost.tech resolves mytestinghost.tech to an IP I can define; 我正在使用javax.ws.rs.client.ClientBuilder创建一个客户端,我希望例如https://mytestinghost.techmytestinghost.tech解析为我可以定义的IP; eg 1.2.3.4 . 例如1.2.3.4

I am either using default connector or Jetty HTTP(2) connector . 我要么使用默认连接器或Jetty HTTP(2)连接器

The client is retrieved using the following code. 使用以下代码检索客户端。

ClientBuilder.newBuilder()
  .trustStore(clientCertificateProvider.getCertificate())
  .withConfig(new ClientConfig().connectorProvider(JettyHttp2Connector::new))

I manage to force the resolution by configuration the underlying SocketAddressResolver inside HttpClient . 我设法通过配置HttpClient的底层SocketAddressResolver强制解析。

ClientBuilder.newBuilder()
  .register(new JacksonJsonProvider())
  .trustStore(HttpUtility.trustStore())
  .withConfig(new ClientConfig().connectorProvider((jaxrsClient, config1) -> {
      final JettyHttp2Connector jettyHttp2Connector = new JettyHttp2Connector(jaxrsClient, config1);
      jettyHttp2Connector.getHttpClient().setSocketAddressResolver((s, i, promise) -> {
          try {
              final List<InetSocketAddress> result = Collections.singletonList(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), managementPort));
              promise.succeeded(result);
          } catch (UnknownHostException e) {
              throw new IllegalStateException(e);
          }

      });
      return jettyHttp2Connector;
  }))

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

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