Custom hostname resolver for JAX-RS client

南楼画角 提交于 2021-01-29 12:22:21

问题


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; e.g. 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))

回答1:


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


来源:https://stackoverflow.com/questions/55609297/custom-hostname-resolver-for-jax-rs-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!