public static void getContacts(WebResource r) { // 1, get response as plain text String jsonRes = r.accept(MediaType.APPLICATION_JSON).get(String.class); System.out.println(jsonRes); String xmlRes = r.accept(MediaType.APPLICATION_XML).get(String.class); System.out.println(xmlRes); // 2, get response and headers etc, wrapped in ClientResponse ClientResponse response = r.get(ClientResponse.class); System.out.println(response.getStatus()); System.out.println(response.getHeaders().get("Content-Type")); String entity = response.getEntity(String.class); System.out.println(entity); // 3, get JAXB response GenericType<List<Contact>> genericType = new GenericType<List<Contact>>() {}; List<Contact> contacts = r.accept(MediaType.APPLICATION_XML).get(genericType); System.out.println("No. of Contacts: " + contacts.size()); Contact contact = contacts.get(0); System.out.println(contact.getId() + ": " + contact.getName()); }
public static void getOneContact(WebResource r, String id) { GenericType<JAXBElement<Contact>> generic = new GenericType<JAXBElement<Contact>>() {}; JAXBElement<Contact> jaxbContact = r.path(id).accept(MediaType.APPLICATION_XML).get(generic); Contact contact = jaxbContact.getValue(); System.out.println(contact.getId() + ": " + contact.getName()); }
public static void putOneContact(WebResource r, Contact c) { ClientResponse response = r.path(c.getId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, c); System.out.println(response.getStatus()); }