Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know a Jersey client proxy implementation like Apache CXF offers for REST

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?

like image 554
Omnaest Avatar asked Oct 24 '11 19:10

Omnaest


2 Answers

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>
like image 121
anre Avatar answered Oct 18 '22 22:10

anre


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.

like image 21
irla Avatar answered Oct 18 '22 23:10

irla