Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I migrate from Jersey 1.0 to Jersey 2.0?

Tags:

I'm trying to upgrade to Jersey 2.0 and I'm having a lot of trouble because the groupIds and artifactIds of Jersey have completely changed and I can't find a migration plan in the Jersey docs.

Here's what my pom.xml used to look like, and this compiled fine:

        <dependency>             <groupId>com.sun.jersey</groupId>             <artifactId>jersey-server</artifactId>             <version>1.17</version>         </dependency>         <dependency>             <groupId>com.sun.jersey</groupId>             <artifactId>jersey-servlet</artifactId>             <version>1.17</version>             <scope>runtime</scope>         </dependency>         <dependency>             <groupId>com.sun.jersey</groupId>             <artifactId>jersey-server-linking</artifactId>             <version>1.17.1</version>         </dependency>         <dependency>             <groupId>com.sun.jersey</groupId>             <artifactId>jersey-client</artifactId>             <version>1.17.1</version>         </dependency> 

What should these be changed to? This unrelated StackOverflow question was somewhat helpful, but I'm having trouble finding things like where the @Ref annotation moved to.


Update

  1. It seems that @Ref no longer exists or at least it's not mentioned in the documentation anymore. Now you use a UriBuilder.
  2. I found a very helpful section in the documentation that answers my maven issues.
  3. The HTTPBasicAuthFilter has been renamed to HttpBasicAuthFilter. Notice the capitalization.
  4. Client client = Client.create(); has become Client client = ClientBuilder.newClient();
  5. This:

        String json = client         .resource(getBaseUrl() + url)         .accept(MediaType.APPLICATION_JSON_TYPE)         .get(String.class); 

    has become

    String json = client         .target(getBaseUrl())         .path(url)         .request(MediaType.APPLICATION_JSON_TYPE)         .get(String.class); 
like image 872
Daniel Kaplan Avatar asked Jun 13 '13 22:06

Daniel Kaplan


People also ask

What is the latest version of Jersey?

The latest stable release of Jersey is 2.37.

What is Jersey API?

Jersey is Sun's production quality reference implementation for JSR 311: JAX-RS: The Java API for RESTful Web Services. Jersey implements support for the annotations defined in JSR-311, making it easy for developers to build RESTful web services with Java and the Java JVM.

What is COM sun Jersey?

com.sun.jersey » jersey-bundleCDDL. A bundle containing code of all jar-based modules that provide JAX-RS and Jersey-related features. Such a bundle is *only intended* for developers that do not use Maven's dependency system.


2 Answers

You don't.

Jersey 2.0 is missing a lot of functionality from Jersey 1.0. Contrary to what the committers will tell you, some things are plain impossible to implement right now (e.g. Guice, Spring integration). Things appear to work on the surface, but once you dig in deeper you will find a lot of features are still broken.

Many of the 1.x plugins do not exist in 2.x, mainly because of the aforementioned breakage.

In light of this, I suggest a holding off on Jersey 2.x for the foreseeable future. Hopefully the committers will clean this up in the coming year.

like image 118
Gili Avatar answered Oct 04 '22 23:10

Gili


It is pain in the neck I have to say. We are currently knee deep into migrating relatively large 3+ years old client-server project and boy do I want to bite my neck off. Hopefully we are at the end of the struggle... While there is a migration guide indeed it is not comprehensive by any means.

  • UniformInterfaceException (and others) is no more.

Instead it is replaced by WebApplication exception and successors. There is not a word about that in the migration guide and this is very very important.

  • JSON support

The migration guide says:

JSON Support has undergone certain changes in Jersey 2.x. The most visible difference for the developer is in the initialization and configuration.

In Jersey 1.x, the JAXB/JSON Support was implemented as a set of MessageBodyReaders and MessageWriters in the jersey-json module. Internally, there were several implementations of JSON to Object mapping ranging from Jersey's own custom solution to third party providers, such as Jackson or Jettison. The configuration of the JSON support was centralized in the JSONConfiguration and JSONJAXBContext classes.

Great. What if you have chosen the "Jersey's own custom solution" (which we did for whatever reason)? There is no alternative to that in jersey 2. I tried to produce the same JSON format using Jettison, Jackson and Moxy providers. I did not succeed. For reference, my unanswered question here: Jersey 2 JSON Jettison unwrapping root element

like image 38
Svilen Avatar answered Oct 04 '22 21:10

Svilen