public void creation() throws URISyntaxException { // tag::creation[] RestClient client = RestClient.create(); // <1> client = RestClient.create("com.holonplatform.jaxrs.client.JaxrsRestClient"); // <2> client = RestClient.forTarget("https://host/api"); // <3> // end::creation[] }
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[] }
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[] }
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[] }