public void asyncCallback() { final Client client = ClientFactory.newClient(); Invocation request = client.request("http://jaxrs.examples.org/jaxrsApplication/customers/{id}").get(); request.pathParam("id", 123); // invoke a request in background client.queue( request, new InvocationCallback<Customer>() { @Override public void onComplete(Future<Customer> future) { // Do something } }); // invoke another request in background Future<?> handle = request .pathParam("id", 456) .queue( new InvocationCallback<HttpResponse>() { @Override public void onComplete(Future<HttpResponse> future) { // do something } }); handle.cancel(true); }
/* * (non-Javadoc) * * @see org.slc.sli.api.client.impl.IRESTClient#deleteRequestWithHeaders(java.net.URL, * java.util.Map) */ @Override public Response deleteRequestWithHeaders(final URL url, final Map<String, Object> headers) throws MalformedURLException, URISyntaxException { if (sessionToken == null) { logger.log( Level.SEVERE, String.format("Token is null in call to RESTClient for url: %s", url.toString())); return null; } Invocation.Builder builder = client.target(url.toURI()).request(MediaType.APPLICATION_JSON); builder = getCommonRequestBuilder(sessionToken, builder, headers); Invocation i = builder.buildDelete(); return i.invoke(); }
public static void main(String... args) { Client client = createClient(); Payment payment = new Payment("123", "567", 10005, "Reimbursement for dinner"); Entity<Payment> entity = Entity.entity(payment, MediaType.APPLICATION_JSON_TYPE); Invocation request = client.target(PAYMENTS_URL).request().buildPut(entity); Response response = request.invoke(); int status = response.getStatus(); if (Response.Status.CREATED.getStatusCode() == status) { System.out.println("Payment sent"); } else { System.out.println("Error sending payment: " + status); } }
/** * We can inject either proxy or a WebTarget for low level manipulations and assertions. * * @param webTarget configured resource ready for use, injected by Arquillian */ @Test public void createCustomerBareJAXRSResource( @ArquillianResteasyResource("rest/customer") JerseyWebTarget webTarget) { // Given final Invocation.Builder invocationBuilder = webTarget.request(); final Invocation invocation = invocationBuilder.buildPost(Entity.entity(new Customer(), MediaType.APPLICATION_JSON_TYPE)); // When final Response response = invocation.invoke(); // Then assertEquals(deploymentURL + "rest/customer", webTarget.getUri().toASCIIString()); assertEquals(MediaType.APPLICATION_JSON, response.getMediaType().toString()); assertEquals(200, response.getStatus()); }