JAX-RS JAVA EE import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response;
public class OrderServiceClient {
public static void main(String[] args) { Client client = ClientBuilder.newClient(); requestAllOrders(client); requestOrderById(client, "343"); requestAllItemsByOrderId(client, "343"); requestItemByOrderIdAndItemId(client, "343", "343");
client.close(); }
private static void requestItemByOrderIdAndItemId(Client client, String orderId, String itemId) { WebTarget target = client.target("http://localhost:8080/jaxrs-client-api-example/api/orders/" + orderId + "/items/" + itemId); //using response object to get more information Response s = target.request().get(); System.out.println("response: " + s.getEntity() + ", status " + s.getStatus()); s.close(); }
private static void requestAllItemsByOrderId(Client client, String orderId) { WebTarget target = client.target("http://localhost:8080/jaxrs-client-api-example/api/orders/" + orderId + "/items"); String s = target.request().get(String.class); System.out.println("response : " + s); }
private static void requestOrderById(Client client, String orderId) { WebTarget target = client.target("http://localhost:8080/jaxrs-client-api-example/api/orders/" + orderId); String s = target.request().get(String.class); System.out.println("response : " + s);
}
private static void requestAllOrders(Client client) { WebTarget target = client.target("http://localhost:8080/jaxrs-client-api-example/api/orders"); String s = target.request().get(String.class); System.out.println("response : " + s); } }
Original Post
import com.logicbig.example.Customer;
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;
public class ClientApp { public static void main(String[] args) { Client client = ClientBuilder.newClient(); WebTarget target = client. target("http://localhost:8080/jaxrs-post-example/api/customers");
postUsingRawJSON(target); postByObjectToJasonTransformation(target); getAllCustomers(target); }
private static void getAllCustomers(WebTarget target) { String s = target.request().get(String.class); System.out.println("All customers : "+s); }
private static void postUsingRawJSON(WebTarget target) { String customer = ClientUtil.createCustomerInJSON("Alyssa William" , "1021 Hweitt Street" , "343-343-3433"); String response = target.request() .post(Entity.entity(customer, MediaType.APPLICATION_JSON) , String.class); System.out.println("customer created with id: "+response);
//get the new customer getCustomerById(target, response);
}
private static void getCustomerById(WebTarget target, String response) {
//the complete resource URI would be //http://localhost:8080/jaxrs-post-example/api/customers/{newId}" String s = target.path(response) .request() .get(String.class); System.out.println("new customer :"+s); }
private static void postByObjectToJasonTransformation(WebTarget target) { Customer newCustomer = ClientUtil.createNewCustomer("Jake Mae", "342, " + "Snake Dr, GreenLake", "444-333-4564");
String response = target.request(MediaType.APPLICATION_JSON) .accept(MediaType.TEXT_PLAIN_TYPE) .post(Entity.json(newCustomer), String.class);
System.out.println("customer created with id: "+response);
//get the new customer getCustomerById(target, response);
} }
Original Post
Sending headers. package com.logicbig.example;
import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget;
public class MyClient { public static void main(String[] args) { Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:8080/test"); Invocation.Builder builder = target.request(); String response = builder.header("myHeader", "a") .header("myHeader", "b") .get(String.class); System.out.println(response); client.close(); } }
Original Post
public class MyClient { public static void main(String[] args) { Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:8080/test2"); Invocation.Builder builder = target.request(); String response = builder.cookie("name1", "value1") .cookie("name2", "value2") .get(String.class); System.out.println(response); client.close(); } }
Original Post
public static <T> T getRequest(String uri, Class<T> responseType) { Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:8080" + uri); return target.request().get(responseType); }
Original Post
public class QueryParamClient { public static void main(String[] args) {
WebTarget target = ClientUtil.createRequest("/test2"); target = target.queryParam("queryParam1", 5); String response = target.request().get(String.class); System.out.printf("response with query param:%n%s%n", response);
target = ClientUtil.createRequest("/test2"); response = target.request().get(String.class); System.out.printf("response without query param:%n%s%n", response); } }
Original Post
public class MatrixParamClient { public static void main(String[] args) {
WebTarget target = ClientUtil.createRequest("/test3"); target = target.matrixParam("matrixParam1", 5); String response = target.request().get(String.class); System.out.printf("response with matrix param:%n%s%n", response);
target = ClientUtil.createRequest("/test3"); response = target.request().get(String.class); System.out.printf("response without matrix param:%n%s%n", response); } }
Original Post
public class FormParamClient { public static void main(String[] args) {
WebTarget target = ClientUtil.createRequest("/test5");
Form form = new Form(); form.param("formParam1", "form param test value");
Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); String response = target.request().post(entity, String.class); System.out.printf("response with form param:%n%s%n", response);
target = ClientUtil.createRequest("/test5"); form = new Form(); entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); response = target.request().post(entity, String.class); System.out.printf("response without form param:%n%s%n", response); } }
Original Post
public class CookieParamClient { public static void main(String[] args) {
WebTarget target = ClientUtil.createRequest("/test4");
String response = target.request() .cookie("cookieParam1", "cookie param value") .get(String.class); System.out.printf("response with cookie param:%n%s%n", response);
//without cookie param target = ClientUtil.createRequest("/test4"); response = target.request().get(String.class); System.out.printf("response without cookie param:%n%s%n", response); } }
Original Post
public class HeaderParamClient { public static void main(String[] args) {
WebTarget target = ClientUtil.createRequest("/test6"); String response = target.request() .header("headerParam1", "header param value") .get(String.class); System.out.printf("response with header param:%n%s%n", response);
target = ClientUtil.createRequest("/test6"); response = target.request().get(String.class); System.out.printf("response without header param:%n%s%n", response); } }
Original Post
How to read headers and entity at the same time? import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response;
public class MyClient3 { public static void main(String[] args) {
Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:8080/orders2"); //to read headers and entity Response r = target.request().get(); MultivaluedMap<String, Object> headers = r.getHeaders(); System.out.println("headers: " + headers); String entity = r.readEntity(String.class); System.out.println(entity); } }
Original Post
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.GenericType; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.Arrays; import java.util.List; import java.util.concurrent.ThreadLocalRandom;
public class EmployeeClient { private static Client client = ClientBuilder.newClient(); private static WebTarget baseTarget = client.target("http://localhost:8080/employees"); private static List<String> deptList = Arrays.asList("Admin", "IT", "Sell");
public static void main(String[] args) { createEmployees(); listEmployees(); deleteEmployeeByIds(2, 4); Employee e = getEmployee(3); e.setDept("Marketing"); updateEmployee(e); listEmployees(); }
private static Employee getEmployee(long id) { System.out.printf("-- getting employee by id: %s --%n", id); Employee e = baseTarget.path(Long.toString(id)) .request(MediaType.APPLICATION_XML) .get(Employee.class); System.out.println("employee retrieved: " + e); return e; }
private static void updateEmployee(Employee e) { System.out.println("-- updating employee --"); System.out.println(e); String response = baseTarget.request(MediaType.APPLICATION_XML) .post(Entity.xml(e), String.class); System.out.println(response); }
private static void deleteEmployeeByIds(long... ids) { System.out.printf("-- deleting employees: %s --%n", Arrays.toString(ids)); for (long id : ids) { WebTarget target = baseTarget.path(Long.toString(id)); Response r = target.request() .delete(); System.out.println(r.readEntity(String.class)); } }
private static void listEmployees() { System.out.println("-- getting employee list --"); List<Employee> employees = baseTarget.request().get( new GenericType<List<Employee>>() { }); employees.forEach(System.out::println); }
private static void createEmployees() { System.out.println("-- making PUT requests --"); for (int i = 1; i <= 5; i++) { Employee e = new Employee(); e.setName("Employee" + i); int index = ThreadLocalRandom.current() .nextInt(0, 3); e.setDept(deptList.get(index));
WebTarget target = baseTarget.path(Integer.toString(i)); Response r = target.request() .put(Entity.entity(e, MediaType.APPLICATION_XML)); System.out.println(r.readEntity(String.class)); } } }
Original Post
public class EmployeeClient { private static Client client = ClientBuilder.newClient(); private static WebTarget baseTarget = client.target("http://localhost:8080/employees");
public static void main(String[] args) { reset(); createEmployees(); listEmployees(); }
private static void listEmployees() { System.out.println("-- getting employee list --"); String response = baseTarget.request().get(String.class); System.out.println(response); }
private static void createEmployees() { System.out.println("-- making PUT requests --");
String[] employeesInYml = { "name: employee1\ndept: IT\n", "name: employee2\ndept: Admin\n", "name: employee3\ndept: Sale\n" };
for (int i = 0; i < employeesInYml.length; i++) { WebTarget target = baseTarget.path(Integer.toString(i)); Response r = target.request() .put(Entity.entity(employeesInYml[i], "application/yaml")); System.out.println(r.readEntity(String.class)); } }
private static void reset() { baseTarget.request().delete(); } }
Original Post
Registering an entity provider on the client side: public class EmployeeClient2 { private static Client client = ClientBuilder.newBuilder().register(new YamlEntityProvider<>()).build(); private static WebTarget baseTarget = client.target("http://localhost:8080/employees");
public static void main(String[] args) { reset(); createEmployees(); listEmployees(); }
private static void listEmployees() { System.out.println("-- getting employee list --"); List<Employee> employees = baseTarget.request().get( new GenericType<List<Employee>>() { }); employees.forEach(System.out::println); }
private static void createEmployees() { System.out.println("-- making PUT requests --");
List<String> deptList = Arrays.asList("Admin", "IT", "Sale"); for (int i = 1; i <= 5; i++) { Employee e = new Employee(); e.setName("Employee" + i); int index = ThreadLocalRandom.current() .nextInt(0, 3); e.setDept(deptList.get(index));
WebTarget target = baseTarget.path(Integer.toString(i)); Response r = target.request() .put(Entity.entity(e, "application/yaml")); System.out.println(r.readEntity(String.class)); } }
private static void reset() { baseTarget.request().delete(); } }
Original Post
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.Form; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.concurrent.Future;
public class MyClient { public static void main(String[] args) throws Exception { Form form = new Form(); form.param("name", null) .param("address", null) .param("phone-number", "343-343-343");
Client client = ClientBuilder.newBuilder().build(); WebTarget target = client.target("http://localhost:8080/customers");
Future<Response> future = target.request(MediaType.APPLICATION_FORM_URLENCODED) .buildPost(Entity.form(form)) .submit(Response.class); Response r = future.get(); if (r.getStatus() != Response.Status.OK.getStatusCode()) { System.out.println(r.getStatus() + " - " + r.getStatusInfo()); }
System.out.println(r.readEntity(String.class)); } }
Original Post
|
|