问题
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