Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use JAX-RS annotated interface with Jersey as the client?

Given that I have an interface which represents my RESET service using

public interface BookResource {

     @GET
     @Path("/book/isbn/{isbn}/")
     @Produces(value = { MediaType.APPLICATION_XML })
     public ClientResponse<Book> getBookByIsbn(@PathParam("isbn") String isbn, @QueryParam("releaseStatus") String releaseStatus);

}

How can I create a proxy to the actual service implementation if I am required to use Jersey as the JAX-RS provider/REST framework in my webapp.

This is easy to do with RESTEasy/Spring integration and means I can use my JAX-RS interface directly without having to wrap it and right boiler plate to do the invocation.

Basically I'm looking for a Jersey equivalent to the following: -

<bean id="bookResource" class="org.jboss.resteasy.client.spring.RestClientProxyFactoryBean">
    <property name="serviceInterface" value="my.company.book.service.BookResource" />
    <property name="baseUri" value="http://localhost:8181/books-service/" />
</bean>

I've just spent the last hour googling this and keep getting back to the standard client API in Jersey which seems to require a lot of boiler plate to achieve the same. Can anyone point me in the right direction?

like image 330
James Avatar asked Jun 12 '13 11:06

James


People also ask

What are JAX-RS annotations used for REST API development?

Jersey Framework is the reference implementation of JAX-RS and it uses all the annotations to build Restful web services. This guide covers all the standard JAX-RS annotations used for Rest API development.

What is JAX-RS client in Jersey?

Overview Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2. For a discussion on the creation of RESTful Web Services using Jersey, please refer to this article.

What is a JAX-RS interface?

JAX-RS is a collection of interfaces and Java™ annotations that simplifies development of server-side REST applications. By using JAX-RS technology, Representational State Transfer (REST) applications are easier to develop and easier to consume when compared to other types of distributed systems.

When are @XmlRootElement annotations like @JAXB required?

JAXB annotations like @XmlRootElement are required only if XML support is needed. 3.2. Creating an Instance of a Client The first thing we need is an instance of a Client:


2 Answers

This link seems to be more practical: http://blog.alutam.com/2012/05/04/proxy-client-on-top-of-jax-rs-2-0-client-api/

// configure Jersey client
ClientConfig cc = new ClientConfig().register(JacksonFeature.class)
            .register(AnotherFeature.class)
            .register(SomeFilter.class);
Client resource = ClientBuilder.newClient(cc);
// create client proxy
ServiceInterface proxy = WebResourceFactory.newResource(ServiceInterface.class,
            resource.target(ServiceURI));

// invoke service
MyType result = proxy.someMethod();

For maven project you would need following dependencies:

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-proxy-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
like image 179
Dmytro Avatar answered Oct 14 '22 22:10

Dmytro


After some further googling I found that the answer is, provided you are using jersey 2.0, to use the jersey proxy-client module which can be found here: -

https://jersey.java.net/project-info/2.0/jersey/project/jersey-proxy-client/dependencies.html

like image 44
James Avatar answered Oct 14 '22 23:10

James