Skip to main content
Last updated June 15, 2011 13:01, by m_potociar
== Low-level JAX-RS 2.0 Client API Samples * Get representation from <nowiki></nowiki> as String: <pre name="java"> Client c = ClientFactory.newClient(); ResourceUri appUri = c.resourceUri(""); String response = appUri.path("helloworld").get().invoke(String.class); </pre> * Get XML representation from <nowiki>http://localhost:8080/sample/helloworld/</nowiki> as String: <pre name="java"> ResourceUri appUri =ClientFactory.newClient().resourceUri(""); String response = appUri.path("helloworld").get() .accept(MediaType.APPLICATION_XML_TYPE) .invoke(String.class); </pre> * Same as last but adding header X-FOO: <pre name="java"> ResourceUri appUri = ClientFactory.newClient().resourceUri(""); String response = appUri.path("helloworld").get() .accept(MediaType.APPLICATION_XML_TYPE).header("X-FOO", "BAR") .invoke(String.class); </pre> * Post text/plain content to <nowiki></nowiki>: <pre name="java"> ResourceUri appUri = ClientFactory.newClient().resourceUri(""); String response = appUri.path("helloworld").post() .entity(“content”).type(MediaType.TEXT_PLAIN_TYPE).invoke(String.class); </pre> * Passing parameters: <pre name="java"> ResourceUri xyzUri = ClientFactory.newClient().resourceUri(""); MultivaluedMap<String, String> params = new MultivaluedHashMap(); params.add(“foo”, “x”); params.add(“bar”, “y”); String response = xyzUri.get().queryParams(params).invoke(String.class); </pre> * Configure client to use filters . Request filtered by filter1, filter2 and filter3; response filtered in reversed order: <pre name="java"> // This example will be updated based on the common interceptors/filters proposal FooFilter filter1 = ...TODO...; BarFilter filter2 = ...TODO...; BazFilter filter3 = getNewFilter(); ClientConfiguration cfg = new DefaultClientConfiguration(); cfg.register(filter1); cfg.register(filter1); cfg.register(filter1); Client c = ClientFactory.newClient(cfg); String response = c.request("").post().entity("hi").invoke(String.class); </pre> * Registering a client-side Provider <tt>MyClassReader</tt> in Client instance: <pre name="java"> ClientConfiguration cfg = new DefaultClientConfiguration(); cfg.register(MyClassReader.class); Client c = ClientFactory.newClient(cfg); MyClass response = c.request("").get().invoke(MyClass.class); </pre> * Async request using Future: <pre name="java"> ResourceUri uri = ClientFactory.newClient().resourceUri(""); Future<String> f = uri.get().queue(String.class); while (!f.isDone()) { System.out.println("Please wait ..."); } System.out.println(f.get()); </pre> * Async request using invocation callback: <pre name="java"> Client c = ClientFactory.newClient(); Future<String> handle = c.request("").get().queue(new AbstractInvocationCallback<String>() { @Override public void onComplete(Future<String> response) { if (!response.isCancelled()) { System.out.println(response.get()); } } }); // something else... if (!handle.isDone()) { System.out.println("Request takes too long. Cancelling..."); handle.cancel(true); } </pre>
Please Confirm