2

Please note: There are similar questions to this, but nothing that makes this an exact dupe.


I currently have the following Gradle dependencies:

dependencies {
    compile "com.sun.jersey.contribs:jersey-apache-client4:1.18.1"
    compile "com.sun.jersey:jersey-client:1.18.1"
    compile "com.sun.jersey:jersey-core:1.18.1"
    compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2"
    compile "org.apache.httpcomponents:httpclient:4.3.2"
    compile "org.slf4j:jcl-over-slf4j:1.7.7"
}

I want to upgrade these to the 2.21 versions of Jersey, however:

  • It seems that the groupName is now org.glassfish.jersey.core instead of com.sun.jersey; and
  • It seems that Jersey-Client now obviates the need to explicitly declare Jersey-Core (Jersey-Core doesn't seem to exist anymore as a JAR)
  • The Jersey Apache 4 Client (jersey-apache-client4) doesn't seem to exist in 2.x land

So I ask, what should all of my 2.21 dependencies be? Where's that Jersey/Apache client hiding out these days?

smeeb
  • 27,777
  • 57
  • 250
  • 447

1 Answers1

3

For Jersey client all you need is

compile 'org.glassfish.jersey.core:jersey-client:2.21'

jersey-core no longer exists, though it can be considered analogous to jersey-common. But you don't need to worry about this. jersey-client pulls it in.

As for Apache, Jersey 2 uses connector providers. See Client Transport Connectors. What you want is the Apache connector

compile 'org.glassfish.jersey.connectors:jersey-apache-connector:2.21'

Then just configure the Client

ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.register(JacksonJsonProvider.class);
Client client = ClientBuilder.newClient(config);

You should also get rid of your current httpclient dependency. jersey-apache-connector pulls it in.

For General usage of the new Client API, see Chapter 5. Client API


Your question title is pretty general, though from the looks of your dependencies you are only trying to implement the client. But for anyone else that wants to implement the server, they can check out

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you (as always) @peeskillet (+1) - however the `jersey-apache-connector` is still giving me trouble. I see it on the [Maven Central repo](http://search.maven.org/#artifactdetails%7Corg.glassfish.jersey.connectors%7Cjersey-apache-connector%7C2.21%7Cjar) however when I add it to my `build.gradle` and run `./gradlew eclipse` it doesn't seem to download the jar. If I go into Eclipse and refresh my project, I get a "*Missing required library*" error. Sure enough, when I go into `~/.gradle/caches/module-2/files-2.1` I see a bunch of Jersey artifacts, but no `jersey-apache-connector`. – smeeb Sep 29 '15 at 08:14
  • Is this jar/pom broken perhaps? My compile declaration is exactly as you posted it...thoughts? Thanks again! – smeeb Sep 29 '15 at 08:14
  • 1
    I didn't test it with Gradle, but it works fine with Maven, and since it _is_ in Maven central, it _should_ work fine with Gradle. Not sure what the problem is. Did you try to get it with Maven into your local maven, and add `mavenLocal()` to your `repositories` in your Gradle build? – Paul Samsotha Sep 29 '15 at 08:18
  • Well now I'm **really** confused. You were right - I didn't have `mavenLocal` nor `mavenCentral` defined in my `repositories`, which is baffling because I'm more than half-way into development and I haven't been having any problems up until now. So *yes*, I am now pulling down the `jersey-apache-connector` (thank you!). **However**, I don't think I'm out of the woods yet... – smeeb Sep 29 '15 at 08:28
  • It looks like there are no longer `get/post/put/delete` methods on [`WebTarget`](https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/client/WebTarget.html), and it doesn't appear that there is a `client.destroy()` method any longer. I'm not finding any docs on the proper way of using `WebTarget`, nor anything on how to politely close the client...any ideas? – smeeb Sep 29 '15 at 08:32
  • See the "Client API" link above. The API has completely changes from Jersey 1x. to Jersey 2.x (which uses a standard JAX-RS API). I can't really give a complete tutorial in this answer, but the documentation I linked to has all the information you need, along with examples. You can see also [this](http://stackoverflow.com/a/27211173/2587435) for some pointers. – Paul Samsotha Sep 29 '15 at 08:34
  • As for the `Client` I don't know if the documentation shows how to close, but it's simply `Client#close()` – Paul Samsotha Sep 29 '15 at 08:41