/** * Invoke the request using <code>DELETE</code> method and receive a response back. * @param <T> Response type * @param responseType Expected response payload type * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default <T> ResponseEntity<T> delete(Class<T> responseType) { return invoke(HttpMethod.DELETE, null, ResponseType.of(responseType)); }
/** * Invoke the request using <code>DELETE</code> method. If the returned response is not a <em>success</em> * response (i.e. with a <code>2xx</code> status code), a {@link UnsuccessfulResponseException} is thrown. * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel) * @throws UnsuccessfulResponseException In case the status code of the response returned by the server is not a * successful type status code, i.e. it is not a <code>2xx</code> status code */ default void deleteOrFail() { invokeForSuccess(HttpMethod.DELETE, null, ResponseType.of(Void.class)); }
/** * Invoke the request using <code>POST</code> method with given <code>entity</code> request payload and receive * the value of the <code>LOCATION</code> header back, if present. * @param entity Request payload * @return the value of the <code>LOCATION</code> header back, if present * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) * @throws UnsuccessfulResponseException In case the status code of the response returned by the server is not a * successful type status code, i.e. it is not a <code>2xx</code> status code */ @Override default Optional<URI> postForLocation(RequestEntity<?> entity) { return invokeForSuccess(HttpMethod.POST, entity, ResponseType.of(Void.class)).getLocation(); }
public void response1() { // tag::response1[] ResponseEntity<TestData> response = RestClient.forTarget("https://rest.api.example/testget").request() .accept(MediaType.APPLICATION_JSON).get(TestData.class); // <1> HttpStatus status = response.getStatus(); // <2> int statusCode = response.getStatusCode(); // <3> long contentLength = response.getContentLength().orElse(-1L); // <4> Optional<String> value = response.getHeaderValue("HEADER_NAME"); // <5> // end::response1[] }
@SuppressWarnings("rawtypes") public void request() { // tag::request[] RequestEntity<String> request1 = RequestEntity.text("test"); // <1> RequestEntity<TestData> request2 = RequestEntity.json(new TestData()); // <2> RequestEntity request3 = RequestEntity .form(RequestEntity.formBuilder().set("value1", "one").set("value2", "a", "b").build()); // <3> // end::request[] }
public void response2() { // tag::response2[] ResponseEntity<TestData> response = RestClient.forTarget("https://rest.api.example/testget").request() .accept(MediaType.APPLICATION_JSON).get(TestData.class); // <1> boolean hasEntity = response.getPayload().isPresent(); // <2> Optional<TestData> entity = response.getPayload(); // <3> Optional<String> asString = response.as(String.class); // <4> // end::response2[] }
/** * Invoke the request using <code>GET</code> method and receive the response entity payload back. * @param <T> Response entity type * @param responseType Expected response payload type * @return the response payload of expected type as the result of the request invocation, or an empty Optional * if not present (empty response entity) * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) * @throws UnsuccessfulResponseException In case the status code of the response returned by the server is not a * successful type status code, i.e. it is not a <code>2xx</code> status code */ @Override default <T> Optional<T> getForEntity(Class<T> responseType) { return invokeForEntity(HttpMethod.GET, null, ResponseType.of(responseType)); }
/** * Create a new {@link RestClient} instance using given <code>classLoder</code> and default implementation, if * available. If more than one {@link RestClient} implementation is found using given ClassLoader, the one returned * by the {@link RestClientFactory} with the higher priority is returned. * @param classLoader The {@link ClassLoader} to use * @return A new {@link RestClient} instance * @throws RestClientCreationException If a {@link RestClient} implementation is not available or a instance * creation error occurred */ static RestClient create(ClassLoader classLoader) { return create(null, classLoader); }
/** * Constructor * @param response Response entity associated to this exception */ public UnsuccessfulResponseException(ResponseEntity<?> response) { super(response.getStatusCode()); this.response = response; }
@SuppressWarnings("unchecked") @Override public Class<? extends T> getPayloadType() throws UnsupportedOperationException { return (Class<? extends T>) (Class<?>) type.getType(); }
/** * Invoke the request using <code>TRACE</code> method and receive a response back. * @param <T> Response type * @param responseType Expected response payload type * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default <T> ResponseEntity<T> trace(Class<T> responseType) { return invoke(HttpMethod.TRACE, null, ResponseType.of(responseType)); }
/** * Invoke the request using <code>OPTIONS</code> method and receive a response back. * @param <T> Response type * @param responseType Expected response payload type * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default <T> ResponseEntity<T> options(Class<T> responseType) { return invoke(HttpMethod.OPTIONS, null, ResponseType.of(responseType)); }
/** * Invoke the request using <code>GET</code> method and receive a response back. * @param <T> Response type * @param responseType Expected response payload type * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default <T> ResponseEntity<T> get(Class<T> responseType) { return invoke(HttpMethod.GET, null, ResponseType.of(responseType)); }
/** * Invoke the request using <code>HEAD</code> method and receive a response back. * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default ResponseEntity<Void> head() { return invoke(HttpMethod.HEAD, null, ResponseType.of(Void.class)); }
/** * Invoke the request using <code>DELETE</code> method and receive a response back. * <p> * The response type is conventionally of {@link Void} type, because no response paylod is expected from this * invocation. Refer to the other <code>post</code> methods to obtain a response payload. * </p> * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel) */ @Override default ResponseEntity<Void> delete() { return invoke(HttpMethod.DELETE, null, ResponseType.of(Void.class)); }
/** * Invoke the request using <code>PUT</code> method with given <code>entity</code> request payload and receive a * response back. * @param <T> Response type * @param entity Request payload * @param responseType Expected response payload type * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default <T> ResponseEntity<T> put(RequestEntity<?> entity, Class<T> responseType) { return invoke(HttpMethod.PUT, entity, ResponseType.of(responseType)); }
/** * Invoke the request using <code>TRACE</code> method and receive a response back. * <p> * The response type is conventionally of {@link Void} type, because no response paylod is expected from this * invocation. Refer to the other <code>post</code> methods to obtain a response payload. * </p> * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel or expected and actual payload type mismatch) */ @Override default ResponseEntity<Void> trace() { return invoke(HttpMethod.TRACE, null, ResponseType.of(Void.class)); }
/** * Invoke the request using <code>OPTIONS</code> method and receive a response back. * <p> * The response type is conventionally of {@link Void} type, because no response paylod is expected from this * invocation. Refer to the other <code>post</code> methods to obtain a response payload. * </p> * @return {@link ResponseEntity} object as a result of the request invocation * @throws HttpClientInvocationException Internal invocation failure (for example, an I/O error on communication * channel) */ @Override default ResponseEntity<Void> options() { return invoke(HttpMethod.OPTIONS, null, ResponseType.of(Void.class)); }