Skip to content

Instantly share code, notes, and snippets.

@aphexmunky
Last active January 21, 2020 11:28
Show Gist options
  • Save aphexmunky/11399575 to your computer and use it in GitHub Desktop.
Save aphexmunky/11399575 to your computer and use it in GitHub Desktop.
Jersey Migration 1 -> 2

Jersey Migration Guide From 1 -> 2

Taken from this link

25.8.2.1. Making a simple client request

Jersey 1.x way:

Client client = Client.create();
WebResource webResource = client.resource(restURL).path("myresource/{param}");
String result = webResource.pathParam("param", "value").get(String.class);

JAX-RS 2.0 way:

Client client = ClientBuilder.newClient();
WebTarget target = client.target(restURL).path("myresource/{param}");

// Currently readEntity causes a memory leak, so read as a string - if you
// need object unmarshalling then use JAX-B
// https://java.net/jira/browse/JERSEY-2463
String result = target.pathParam("param", "value").get(String.class);

25.8.2.2. Registering filters

Jersey 1.x way:

Client client = Client.create();
WebResource webResource = client.resource(restURL);
webResource.addFilter(new HTTPBasicAuthFilter(username, password));

JAX-RS 2.0 way:

Client client = ClientBuilder.newClient();
WebTarget target = client.target(restURL);
target.register(new HttpBasicAuthFilter(username, password));

25.8.2.3. Setting "Accept" header

Jersey 1.x way:

Client client = Client.create();
WebResource webResource = client.resource(restURL).accept("text/plain");
ClientResponse response = webResource.get(ClientResponse.class);

JAX-RS 2.0 way:

Client client = ClientBuilder.newClient();
WebTarget target = client.target(restURL);
Response response = target.request("text/plain").get();

25.8.2.4. Attaching entity to request

Jersey 1.x way:

Client client = Client.create();
WebResource webResource = client.resource(restURL);
ClientResponse response = webResource.post(ClientResponse.class, "payload");

JAX-RS 2.0 way:

Client client = ClientBuilder.newClient();
WebTarget target = client.target(restURL);
Response response = target.request().post(Entity.text("payload"));

25.8.2.5. Setting SSLContext and/or HostnameVerifier

Jersey 1.x way:

HTTPSProperties prop = new HTTPSProperties(hostnameVerifier, sslContext);
DefaultClientConfig dcc = new DefaultClientConfig();
dcc.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, prop);
Client client = Client.create(dcc);

Jersey 2.0 way:

Client client = ClientBuilder.newBuilder()
        .sslContext(sslContext)
        .hostnameVerifier(hostnameVerifier)
        .build();
@tariq1890
Copy link

Thanks for this!

@abhisheksingh03
Copy link

Thanks dude. It really helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment