Enterprise Java

Jersey client dependencies for JAX-RS 2.1

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. 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'

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java:

Success! Now check your email to confirm your subscription.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Jersey client dependencies for JAX-RS 2.1

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button