/** * 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); }
/** * 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)); }
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[] }
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[] }