/** * Does HTTP POST invocation and returns typed response object * @param body request body, can be null * @param responseClass expected type of response object * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T post(Object body, Class<T> responseClass) { return invoke(HttpMethod.POST, body, responseClass); }
/** * Does HTTP PUT invocation and returns typed response object * @param body request body, can be null * @param responseClass expected type of response object * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T put(Object body, Class<T> responseClass) { return invoke(HttpMethod.PUT, body, responseClass); }
/** * Does HTTP GET invocation and returns typed response object * @param responseType generic response type * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T get(GenericType<T> responseType) { return invoke(HttpMethod.GET, null, responseType); }
/** * Does HTTP POST invocation * @param body request body, can be null * @return JAXRS Response */ public Response post(Object body) { return invoke(HttpMethod.POST, body); }
/** * Does HTTP DELETE invocation * @return JAXRS Response */ public Response delete() { return invoke(HttpMethod.DELETE, null); }
/** * Does HTTP PUT invocation and returns typed response object * @param body request body, can be null * @param responseClass expected type of response object * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T put(Object body, Class<T> responseClass) { return invoke("PUT", body, responseClass); }
/** * Does HTTP DELETE invocation * @return JAXRS Response */ public Response delete() { return invoke("DELETE", null); }
/** * Does HTTP PUT invocation and returns typed response object * @param body request body, can be null * @param responseType generic response type * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T put(Object body, GenericType<T> responseType) { return invoke("PUT", body, responseType); }
/** * Does HTTP PUT invocation * @param body request body, can be null * @return JAXRS Response */ public Response put(Object body) { return invoke(HttpMethod.PUT, body); }
@Override public <T> T method(String method, Entity<?> entity, GenericType<T> genericType) { return wc.invoke(method, entity, genericType); }
/** * Does HTTP PUT invocation * @param body request body, can be null * @return JAXRS Response */ public Response put(Object body) { return invoke(HttpMethod.PUT, body); }
/** * Does HTTP GET invocation * @return JAXRS Response */ public Response get() { return invoke(HttpMethod.GET, null); }
/** * Does HTTP PUT invocation and returns typed response object * @param body request body, can be null * @param responseClass expected type of response object * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T put(Object body, Class<T> responseClass) { return invoke(HttpMethod.PUT, body, responseClass); }
/** * Does HTTP HEAD invocation * @return JAXRS Response */ public Response head() { return invoke(HttpMethod.HEAD, null); }
/** * Does HTTP PUT invocation and returns typed response object * @param body request body, can be null * @param responseType generic response type * @return typed object, can be null. Response status code and headers * can be obtained too, see Client.getResponse() */ public <T> T put(Object body, GenericType<T> responseType) { return invoke(HttpMethod.PUT, body, responseType); }
@Test public void testPostBookXsiType() throws Exception { String address = "http://localhost:" + PORT + "/the/thebooksxsi/bookstore/books/xsitype"; JAXBElementProvider<Book> provider = new JAXBElementProvider<>(); provider.setExtraClass(new Class[]{SuperBook.class}); provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName())); WebClient wc = WebClient.create(address, Collections.singletonList(provider)); wc.accept("application/xml"); wc.type("application/xml"); SuperBook book = new SuperBook("SuperBook2", 999L, true); Book book2 = wc.invoke("POST", book, Book.class, Book.class); assertEquals("SuperBook2", book2.getName()); }
@Test public void testReplaceBookMistypedCTAndHttpVerb2() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books2/mistyped"; WebClient wc = WebClient.create(endpointAddress, Collections.singletonList(new ReplaceBodyFilter())); wc.accept("text/mistypedxml").header("THEMETHOD", "PUT"); Book book = wc.invoke("GET", null, Book.class); assertEquals(561L, book.getId()); }
@Test public void testReplaceBookMistypedCTAndHttpVerb() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books2/mistyped"; WebClient wc = WebClient.create(endpointAddress, Collections.singletonList(new ReplaceBodyFilter())); wc.accept("text/mistypedxml").type("text/xml").header("THEMETHOD", "PUT"); Book book = wc.invoke("DELETE", new Book("book", 555L), Book.class); assertEquals(561L, book.getId()); } @Test
@Test public void testDeleteWithBody() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/deletebody"; WebClient wc = WebClient.create(address); wc.type("application/xml").accept("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("DELETE", new Book("Delete", 123L), Book.class); assertEquals("Delete", book.getName()); wc.close(); }
@Test public void testRetrieveBookCustomMethodAsyncSync() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); // Setting this property is not needed given that // we have the async conduit loaded in the test module: // WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); wc.type("application/xml").accept("application/xml"); Book book = wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class); assertEquals("Retrieve", book.getName()); wc.close(); }