yaml
httpClient:
connectTimeout: 5000
readTimeout: 10000
maxConnections: 100
public class MyApplication extends Application<MyConfiguration> {
@Override
public void run(MyConfiguration configuration, Environment environment) {
HttpClient httpClient = new HttpClientBuilder(environment).using(configuration.getHttpClient()).build("my-http-client");
environment.jersey().register(new MyResource(httpClient));
}
public static void main(String[] args) throws Exception {
new MyApplication().run(args);
}
}
@Path("/my-resource")
public class MyResource {
private final HttpClient httpClient;
public MyResource(HttpClient httpClient) {
this.httpClient = httpClient;
}
@GET
public Response get() {
String url = "https://api.example.com/data";
Request request = httpClient.target(url).request();
try {
Response response = request.get();
return response;
} catch (ProcessingException e) {
return Response.serverError().entity("An error occurred.").build();
}
}
}