the Apache CXF project offers a proxy based client implementation for REST services. This looks like:
Resource resource = JAXRSClientFactory.create( baseAddress, Resource.class )
Does anyone know a similar implementation for Jersey?
I spotted an approach using @HyperMediaController
annotations, but I want to stick to JSR-311 default annotations like @Path
and @Get
...
Has anyone an idea?
A proxy implementation exists, but unfortunately it's not even mentioned in Jersey Client API documentation (neither in Jersey User Guide) as of version 2.22.1.
What I found was JavaDoc for WebResourceFactory, even better is the package JavaDoc. Here's a snippet from the JavaDoc on the usage of the WebResourceFactory:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/");
MyResourceIfc resource = WebResourceFactory.newResource(MyResourceIfc.class, target);
In Maven you then need:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-proxy-client</artifactId>
<version>2.22.1</version>
</dependency>
in addition to
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
I found WebResourceFactory miss generic types supports and it's source code was really hard to understand. So we created https://github.com/adaptris/jaxrs-client-proxy and we are currently devloping it.
To use it you need to build a resource:
ResourceBuilder builder = new ResourceBuilder();
resource = builder.
url("https://host/api").
build(Resource.class);
client = resource.get();
Then you can call client
- which is proxy of your jax-rs annotation described interface (Resource.class
). You should close a resource after stoping using it as it is recommended by jax-rs client api.
resource.close()
More details on github projet page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With