Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package rest; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import objects.Person; import objects.Project; import org.glassfish.jersey.client.ClientConfig; /** * * @author daveyle */ public class ProjectREST { public Project getSingleProject(String projectID) { ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(UriBuilder.fromUri(Utils.mainURL + "/project").build()); Response newResponse = null; newResponse = target.path("/" + projectID).request().get(Response.class); return getFormattedDataSingle(newResponse); } public List<Project> getAllProjects() { ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(UriBuilder.fromUri(Utils.mainURL + "/project").build()); Response newResponse = null; newResponse = target.queryParam("show_hidden", true).queryParam("time", null).request().get(Response.class); return getFormattedDataMany(newResponse); } private Project getFormattedDataSingle(Response response) { String responseJson = response.readEntity(String.class); System.out.println(responseJson); ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() { }; StringBuilder sb = new StringBuilder(); try { HashMap<String, Object> map = mapper.readValue(responseJson, typeReference); if (Boolean.parseBoolean(map.get("found").toString())) { return Project.parseProject(map); // } else { return null; } } catch (Exception e) { return null; } } private List<Project> getFormattedDataMany(Response response) { List<Project> projects = new ArrayList<Project>(); String responseJson = response.readEntity(String.class); System.out.println(responseJson); ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() { }; try { HashMap<String, Object> o = mapper.readValue(responseJson, typeReference); ArrayList<HashMap<String, Object>> list = (ArrayList) ((HashMap) o.get("hits")).get("hits"); for (HashMap<String, Object> map : list) { Project p = Project.parseProject(map); projects.add(p); } } catch (IOException e) { e.printStackTrace(); } return projects; } }