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?
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.
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.
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.
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:
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>
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
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