public void defaults() throws URISyntaxException { // tag::defaults[] RestClient client = RestClient.create(); client.defaultTarget(new URI("https://rest.api.example")); // <1> client.withDefaultHeader(HttpHeaders.ACCEPT_LANGUAGE, "en-CA"); // <2> client.withDefaultHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"); // <3> // end::defaults[] }
/** * Create a new {@link RestClient} instance using default {@link ClassLoader} and default implementation, setting * given <code>baseUri</code> as default {@link RestClient} target, which will be used as base URI for every request * configured using {@link #request()}, if not overridden using {@link RequestDefinition#target(URI)}. * @param baseUri The base target URI of the returned {@link RestClient} * @return A new {@link RestClient} instance */ static RestClient forTarget(URI baseUri) { return create().defaultTarget(baseUri); }
public void restClient() throws URISyntaxException { // tag::restclient[] final PathProperty<Integer> ID = PathProperty.create("id", Integer.class); final PathProperty<String> NAME = PathProperty.create("name", String.class); final PropertySet<?> PROPERTY_SET = PropertySet.of(ID, NAME); RestClient client = JaxrsRestClient.create() // <1> .defaultTarget(new URI("https://host/api")); // <2> client = RestClient.create(JaxrsRestClient.class.getName()); // <3> client = RestClient.create(); // <4> client = RestClient.forTarget("https://host/api"); // <5> Optional<TestData> testData = client.request().path("data/{id}").resolve("id", 1) // <6> .accept(MediaType.APPLICATION_JSON).getForEntity(TestData.class); Optional<PropertyBox> box = client.request().path("getbox") // <7> .propertySet(PROPERTY_SET).getForEntity(PropertyBox.class); HttpResponse<PropertyBox> response = client.request().path("getbox") // <8> .propertySet(PROPERTY_SET).get(PropertyBox.class); List<PropertyBox> boxes = client.request().path("getboxes") // <9> .propertySet(PROPERTY_SET).getAsList(PropertyBox.class); PropertyBox postBox = PropertyBox.builder(PROPERTY_SET).set(ID, 1).set(NAME, "Test").build(); HttpResponse<Void> postResponse = client.request().path("postbox") // <10> .post(RequestEntity.json(postBox)); // end::restclient[] }
public void configuration() throws URISyntaxException { RestClient client = RestClient.create(); RequestDefinition request = client.request().target(URI.create("https://rest.api.example")); // <1> request = request.path("apimethod"); // <2> request = request.path("subpath"); // <3> client.request().target("https://rest.api.example").path("/data/{name}/{id}").resolve("name", "test") .resolve("id", 123); // <1> request = client.request().target("https://rest.api.example").path("/test/{id}").resolve(templates); // <2> client.request().queryParameter("parameter", "value") // <1> .queryParameter("multiValueParameter", 1, 2, 3); // <2> client.request().header("Accept", "text/plain"); // <1> client.request().header(HttpHeaders.ACCEPT, "text/plain"); // <2> client.request().accept("text/plain", "text/xml"); // <3> client.request().accept(MediaType.APPLICATION_JSON); // <4> client.request().acceptEncoding("gzip"); // <5> client.request().acceptCharset("utf-8"); // <6> client.request().acceptCharset(Charset.forName("utf-8")); // <7> client.request().acceptLanguage("en-CA"); // <8> client.request().acceptLanguage(Locale.US, Locale.GERMANY); // <9> client.request().cacheControl(CacheControl.builder().noCache(true).noStore(true).build()); // <10> client.request().authorizationBasic("username", "password"); // <1> client.request().authorizationBearer("An389fz56xsr7"); // <2>
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[] }
/** * 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); }
/** * Create a new {@link RestClient} instance using default {@link ClassLoader} and default implementation, setting * given <code>baseUri</code> as default {@link RestClient} target, which will be used as base URI for every request * configured using {@link #request()}, if not overridden using {@link RequestDefinition#target(URI)}. * @param baseUri The base target URI of the returned {@link RestClient} * @return A new {@link RestClient} instance */ static RestClient forTarget(String baseUri) { return create().defaultTarget(URI.create(baseUri)); }
@SuppressWarnings("unchecked") public void propertiesInvocation() { // tag::properties[] final PathProperty<Integer> CODE = create("code", int.class); final PathProperty<String> VALUE = create("value", String.class); final PropertySet<?> PROPERTIES = PropertySet.of(CODE, VALUE); RestClient client = RestClient.create(); PropertyBox box = client.request().target("https://rest.api.example").path("/apimethod").propertySet(PROPERTIES) .getForEntity(PropertyBox.class).orElse(null); // <1> Optional<PropertyBox> box2 = client.request().target("https://rest.api.example").path("/apimethod") .propertySet(CODE, VALUE).getForEntity(PropertyBox.class); // <2> List<PropertyBox> boxes = client.request().target("https://rest.api.example").path("/apimethod") .propertySet(PROPERTIES).getAsList(PropertyBox.class); // <3> // end::properties[] }
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("resource") public void invocationMethods() { final RestClient client = RestClient.forTarget("https://rest.api.example/test"); ResponseEntity<TestData> response = client.request().get(TestData.class); // <1> response = client.request().get(ResponseType.of(TestData.class)); // <2> response = client.request().put(RequestEntity.json(new TestData()), TestData.class); // <3> ResponseEntity<Void> response2 = client.request().post(RequestEntity.json(new TestData())); // <1> Optional<TestData> value = client.request().getForEntity(TestData.class); // <1> Optional<List<TestData>> values = client.request().getForEntity(ResponseType.of(TestData.class, List.class)); // <2> client.request().getForEntity(TestData.class); } catch (UnsuccessfulResponseException e) { InputStream responseEntityStream = client.request().getForStream(); List<TestData> collectionOfValues = client.request().getAsList(TestData.class); Optional<URI> locationHeaderURI = client.request().postForLocation(RequestEntity.json(new TestData())); RestClient restClient = RestClient.forTarget("http://api.example"); // Obtain a RestClient restClient.request(); // Request definition
/** * Create a new {@link RestClient} instance using default {@link ClassLoader} and the implementation whith given * fully qualified class name. * @param fullyQualifiedClassName The {@link RestClient} implementation fully qualified class name to obtain * @return A new {@link RestClient} instance * @throws RestClientCreationException If the implementation which corresponds to given fully qualified class name * is not available or a instance creation error occurred */ static RestClient create(String fullyQualifiedClassName) { return create(fullyQualifiedClassName, ClassUtils.getDefaultClassLoader()); }
private void getClient() { Client jaxrsClient = clientBuilder.build(); // <1> RestClient restClient = RestClient.create(); // <2> } // end::clientbuilder2[]
/** * Create a new {@link RestClient} instance using default {@link ClassLoader} 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. * @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() { return create(null, ClassUtils.getDefaultClassLoader()); }