Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jersey Client Dependencies for JAX-RS 2.1

DZone's Guide to

Jersey Client Dependencies for JAX-RS 2.1

If you're looking to make use of a JAX-RS client outside of an enterprise container, you'll need to know the necessary dependencies.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Jersey is the reference implementation of JAX-RS 2.1. The following Jersey dependencies are required in order to run a JAX-RS 2.1 client with JSON-P and JSON-B mapping outside of an enterprise container.

Jersey client version 2.6 implements the JAX-RS 2.1 API. The following dependencies add the client runtime to a project:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>


If JSON objects should be mapped using JSON-P, the following dependency is required as well:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.26</version>
</dependency>


This already adds an implementation for JSON-P 1.1, namely Glassfish javax.json.

If JSON objects should be mapped using JSON-B, the following dependency is added instead of or additionally to the previous one:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-binding</artifactId>
    <version>2.26</version>
</dependency>


This transitively adds the Yasson dependency, the reference implementation of JSON-B.

These dependencies enable the project to use the JAX-RS 2.1 client together with JSON-P or JSON-B binding:

Client client = ClientBuilder.newClient();
WebTarget target = client
        .target("http://localhost:8080/jersey-test/resources/tests");

Response response = target.request(MediaType.APPLICATION_JSON_TYPE).get();
JsonArray customers = response.readEntity(JsonArray.class);

response = target.path("123").request(MediaType.APPLICATION_JSON_TYPE).get();
Customer customer = response.readEntity(Customer.class);

...

public class Customer {

    @JsonbTransient
    private long id;
    private String name;

    // getters & setters
}


And for our Gradle users, here is the equivalent of the Maven declarations:

compile 'org.glassfish.jersey.core:jersey-client:2.26'
compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'

compile 'org.glassfish.jersey.media:jersey-media-json-processing:2.26'
compile 'org.glassfish.jersey.media:jersey-media-json-binding:2.26'

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
jersey ,dependencies ,java ,maven ,gradle ,json-b ,json-p ,tutorial

Published at DZone with permission of Sebastian Daschner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}