Skip to content

Instantly share code, notes, and snippets.

@kbilel
Created April 26, 2018 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbilel/14873d4f7595aefe2ec9cba929daff7d to your computer and use it in GitHub Desktop.
Save kbilel/14873d4f7595aefe2ec9cba929daff7d to your computer and use it in GitHub Desktop.
Search
HOME RESUME PYTHON SAAS GIT OTHER POEMS LOG IN
JERSEY CLIENT FRAMEWORK
Simple client to invoke a GET request
package client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class CleintGet {
public static void main(String[] args) {
try {
//create a client object for consuming the REST response
Client client = ClientBuilder.newClient();
//the REST URL you want to connect to
WebTarget target = client.target("http://hit.run.aws-usw02-pr.ice.predix.io/rest/myresource/999999999999,111111111111");
//tell the service you are connecting to that you accept JSON
//and invoke the GET
Response response = target.request(MediaType.APPLICATION_JSON_TYPE).get();
//responseStr now holds the response from the REST service
String responseStr = response.readEntity(String.class);
System.out.println(responseStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
TRYIT: Modify this code to get a song by id, ex: http://localhost:8080/unh/rest/song/3
The WebTarget can be reused by specifying a root of the service and then appending it with "path" string
WebTarget target = client.target("http://localhost:8080/unh/rest");
Response response = target.path("song").request(MediaType.APPLICATION_JSON_TYPE).get();
TRYIT: Modify your application to use the path method for target
locating resource and adding query parameter:
WebTarget target = client.target("http://api.openweathermap.org/data/2.5");
Response response = target.path("weather").queryParam("q","London,uk").request(MediaType.APPLICATION_JSON_TYPE).get();
Passing more query parameters and parsing the output:
package client;
import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
public class WeatherGet {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
Client client = ClientBuilder.newClient();
WebTarget target = client
.target("http://api.openweathermap.org/data/2.5");
Response response = target.path("weather")
.queryParam("q", "London,uk")
.queryParam("units", "metric")
.queryParam("APPID", "b504dd6600d713651dc073eb660a2664")
.request(MediaType.APPLICATION_JSON_TYPE).get();
String responseStr = response.readEntity(String.class);
try {
ObjectMapper om = new ObjectMapper();
HashMap<String, Object> hm = (HashMap<String, Object>) om
.readValue(responseStr, HashMap.class);
HashMap<String, Double> mainMap = (HashMap<String, Double>)hm.get("main");
System.out.println(mainMap.get("temp"));
} catch (Exception ex) {
System.out.println("Could not parse the output");
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
a helpful tool for unmarshalling JSON to POJO: http://www.jsonschema2pojo.org/
An Example of parsing JSON response
package client;
import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HelloWeather {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
Client client = ClientBuilder.newClient();
//http://api.openweathermap.org/data/2.5/weather?q=London,uk
WebTarget target = client
.target("http://api.openweathermap.org/data/2.5");
Response response = target.path("weather")
.queryParam("q", "London,uk").queryParam("units", "metric")
.queryParam("APPID", "b504dd6600d713651dc073eb660a2664")
.request(MediaType.APPLICATION_JSON_TYPE).get();
String responseStr = response.readEntity(String.class);
try {
ObjectMapper om = new ObjectMapper();
HashMap<String, Object> hm = (HashMap<String, Object>) om
.readValue(responseStr, HashMap.class);
Double temp = ((HashMap<String, Double>) hm.get("main"))
.get("temp");
String name = (String)hm.get("name");
StringBuilder sb = new StringBuilder();
sb.append("Hello ");
sb.append(name);
sb.append(" it's ");
sb.append(temp);
sb.append(" degrees C");
System.out.println(sb);
} catch (Exception ex) {
System.out.println("Could not parse the output");
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hello London it's 7.9 degrees C
POST CLIENT
package client;
import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PostBook {
public static void main(String[] args) {
try {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/jersey_mongo/rest");
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("title", "BOOK_POST");
hm.put("author", "AUTHOR_POST");
ObjectMapper om = new ObjectMapper();
Response r = target.path("books").request(MediaType.APPLICATION_JSON_TYPE).post(
Entity.entity(om.writeValueAsString(hm), MediaType.APPLICATION_JSON_TYPE));
System.out.println(r.getStatus());
System.out.println(r.readEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
DELETE CLIENT
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class ClientDelete {
public static void main(String[] args) {
try {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/unh/rest");
Response r = target.path("song").path("9")
.request(MediaType.APPLICATION_JSON_TYPE).delete();
System.out.println(r.getStatus());
System.out.println(r.readEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
TRYIT: write update client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment