简体   繁体   中英

Custom hostname resolver for JAX-RS client

Is there a way to customize the host name resolution inside a JAX-RS client?

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; eg 1.2.3.4 .

I am either using default connector or Jetty HTTP(2) connector .

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 .

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;
  }))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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