本文整理汇总了Java中com.holonplatform.http.rest.RequestEntity类的典型用法代码示例。如果您正苦于以下问题:Java RequestEntity类的具体用法?Java RequestEntity怎么用?Java RequestEntity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RequestEntity类属于com.holonplatform.http.rest包,在下文中一共展示了RequestEntity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invoke
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Override
public <T, R> ResponseEntity<T> invoke(RequestDefinition requestDefinition, HttpMethod method,
RequestEntity<R> requestEntity, ResponseType<T> responseType, boolean onlySuccessfulStatusCode) {
// invocation builder
final Builder builder = configure(requestDefinition).request();
// headers
requestDefinition.getHeaders().forEach((n, v) -> builder.header(n, v));
// invocation
final javax.ws.rs.client.Invocation invocation = buildRequestEntity(requestEntity)
.map(r -> builder.build(method.getMethodName(), r)).orElse(builder.build(method.getMethodName()));
// invoke
Response response = null;
try {
response = invocation.invoke();
} catch (Exception e) {
throw new HttpClientInvocationException(e);
}
if (response == null) {
throw new HttpClientInvocationException("Invocation returned a null Response");
}
// check error status code
if (onlySuccessfulStatusCode && !HttpStatus.isSuccessStatusCode(response.getStatus())) {
throw new UnsuccessfulResponseException(new JaxrsRawResponseEntity(response));
}
return new JaxrsResponseEntity<>(response, responseType);
}
示例2: buildRequestEntity
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
/**
* Build a jax-rs {@link Entity} from given request entity
* @param requestEntity Request entity
* @return jax-rs Entity
*/
protected Optional<Entity<?>> buildRequestEntity(RequestEntity<?> requestEntity) {
if (requestEntity != null) {
boolean form = requestEntity.getMediaType().map(m -> APPLICATION_FORM_URLENCODED_MEDIA_TYPE.equals(m))
.orElse(Boolean.FALSE);
return requestEntity.getPayload().map(p -> form ? Entity.form(convert(HttpUtils.getAsMultiMap(p)))
: Entity.entity(p, requestEntity.getMediaType().orElse(null)));
}
return Optional.empty();
}
示例3: testMethods
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testMethods() {
final RestClient client = RestClient.forTarget(getBaseUri());
HttpResponse<?> rsp = client.request().path("test").path("data/save")
.put(RequestEntity.json(new TestData(7, "testPost")));
assertNotNull(rsp);
assertEquals(HttpStatus.ACCEPTED, rsp.getStatus());
rsp = client.request().path("test").path("formParams")
.post(RequestEntity.form(RequestEntity.formBuilder().set("one", "1").set("two", "1").build()));
assertNotNull(rsp);
assertEquals(HttpStatus.OK, rsp.getStatus());
}
示例4: testErrors
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testErrors() {
final RestClient client = JaxrsRestClient.create(getClient()).defaultTarget(getBaseUri());
ResponseEntity<?> rsp = client.request().path("test").path("data/save")
.put(RequestEntity.json(new TestData(-1, "testErr")));
assertNotNull(rsp);
assertEquals(HttpStatus.BAD_REQUEST, rsp.getStatus());
ApiError error = rsp.as(ApiError.class).orElse(null);
assertNotNull(error);
assertEquals("ERR000", error.getCode());
ResponseEntity<TestData> r2 = client.request().path("test").path("data2/{id}").resolve("id", -1)
.get(TestData.class);
assertNotNull(r2);
assertEquals(HttpStatus.BAD_REQUEST, r2.getStatus());
error = r2.as(ApiError.class).orElse(null);
assertNotNull(error);
assertEquals("ERR000", error.getCode());
TestUtils.expectedException(UnsuccessfulResponseException.class, () -> {
client.request().path("test").path("data2/{id}").resolve("id", -1).getForEntity(TestData.class)
.orElse(null);
});
try {
client.request().path("test").path("data2/{id}").resolve("id", -1).getForEntity(TestData.class)
.orElse(null);
} catch (UnsuccessfulResponseException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatus().orElse(null));
assertNotNull(e.getResponse());
ApiError err = e.getResponse().as(ApiError.class).orElse(null);
assertNotNull(err);
assertEquals("ERR000", err.getCode());
}
}
示例5: getRequestPayload
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
/**
* Get the request entity payload
* @param requestEntity RequestEntity
* @return Request entity payload, may be null
*/
protected Object getRequestPayload(RequestEntity<?> requestEntity) {
if (requestEntity != null) {
boolean form = requestEntity.getMediaType().map(m -> APPLICATION_FORM_URLENCODED_MEDIA_TYPE.equals(m))
.orElse(Boolean.FALSE);
return requestEntity.getPayload().map(p -> form ? new LinkedMultiValueMap<>(HttpUtils.getAsMultiMap(p)) : p)
.orElse(null);
}
return null;
}
示例6: testClient
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testClient() {
final RestClient client = SpringRestClient.create(restTemplate).defaultTarget(getBaseUri());
TestData td = client.request().path("test").path("data/{id}").resolve("id", 1).getForEntity(TestData.class)
.orElse(null);
assertNotNull(td);
assertEquals(1, td.getCode());
List<TestData> tds = client.request().path("test").path("data").getAsList(TestData.class);
assertNotNull(td);
assertEquals(2, tds.size());
ResponseEntity<?> rspe = client.request().path("test").path("data/{id}").resolve("id", 1).get(TestData.class);
assertEquals(TestData.class, rspe.getPayloadType());
String asString = rspe.as(String.class).orElse(null);
assertNotNull(asString);
assertNotNull(rspe.getHeaders());
HttpResponse<?> rsp = client.request().path("test").path("data/save")
.put(RequestEntity.json(new TestData(7, "testPost")));
assertNotNull(rsp);
assertEquals(HttpStatus.ACCEPTED, rsp.getStatus());
rsp = client.request().path("test").path("formParams")
.post(RequestEntity.form(RequestEntity.formBuilder().set("one", "1").set("two", "1").build()));
assertNotNull(rsp);
assertEquals(HttpStatus.OK, rsp.getStatus());
}
示例7: methods
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
public void methods() {
// tag::post1[]
ResponseEntity<Void> response1 = RestClient.forTarget("https://rest.api.example/testpost").request()
.post(RequestEntity.json(new TestData()));
ResponseEntity<TestData> response2 = RestClient.forTarget("https://rest.api.example/testpost").request()
.post(RequestEntity.json(new TestData()), TestData.class);
ResponseEntity<List<TestData>> response3 = RestClient.forTarget("https://rest.api.example/testpost").request()
.post(RequestEntity.json(new TestData()), ResponseType.of(TestData.class, List.class));
// end::post1[]
// tag::get1[]
try {
Optional<TestData> data = RestClient.forTarget("https://rest.api.example/testget").request()
.getForEntity(TestData.class);
final ResponseType<List<TestData>> responseType = ResponseType.of(TestData.class, List.class);
List<TestData> dataList = RestClient.forTarget("https://rest.api.example/testgetlist").request()
.getForEntity(responseType).orElse(Collections.emptyList());
} catch (UnsuccessfulResponseException e) {
// got a response with a status code different from 2xx
int httpStatusCode = e.getStatusCode();
e.getStatus().ifPresent(status -> System.err.println(status.getDescription()));
ResponseEntity<?> theResponse = e.getResponse();
}
// end::get1[]
}
示例8: request
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@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[]
}
示例9: testClient
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testClient() {
final RestClient client = JaxrsRestClient.create(getClient()).defaultTarget(getBaseUri());
TestData td = client.request().path("test").path("data/{id}").resolve("id", 1)
.accept(MediaType.APPLICATION_JSON).getForEntity(TestData.class).orElse(null);
assertEquals(1, td.getCode());
HttpResponse<TestData> rsp = client.request().path("test").path("data/{id}").resolve("id", 1)
.get(TestData.class);
assertEquals(HttpStatus.OK, rsp.getStatus());
assertEquals(Integer.valueOf(1), rsp.getPayload().map(p -> p.getCode()).orElse(null));
PropertyBox box = client.request().path("test").path("box/{id}").resolve("id", 1).propertySet(PROPERTIES)
.getForEntity(PropertyBox.class).orElse(null);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
HttpResponse<PropertyBox> rsp2 = client.request().path("test").path("box/{id}").resolve("id", 1)
.propertySet(PROPERTIES).get(PropertyBox.class);
assertEquals(HttpStatus.OK, rsp2.getStatus());
box = rsp2.getPayload().orElse(null);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
List<PropertyBox> boxes = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class);
assertNotNull(boxes);
assertEquals(2, boxes.size());
box = boxes.get(0);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
box = boxes.get(1);
assertNotNull(box);
assertEquals(new Integer(2), box.getValue(CODE));
assertEquals("value2", box.getValue(VALUE));
List<Integer> codes = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class).stream().map(p -> p.getValue(CODE)).collect(Collectors.toList());
assertNotNull(codes);
assertEquals(2, codes.size());
List<String> values = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class).stream().map(p -> p.getValue(VALUE)).collect(Collectors.toList());
assertNotNull(values);
assertEquals(2, values.size());
HttpResponse<TestData> prsp = client.request().path("test").path("postdata").queryParameter("id", 1)
.post(RequestEntity.EMPTY, TestData.class);
assertEquals(HttpStatus.OK, prsp.getStatus());
assertTrue(prsp.getPayload().isPresent());
PropertyBox postBox = PropertyBox.builder(PROPERTIES).set(CODE, 100).set(VALUE, "post").build();
HttpResponse<Void> postResponse = client.request().path("test").path("box/post")
.post(RequestEntity.json(postBox));
assertEquals(HttpStatus.ACCEPTED, postResponse.getStatus());
}
示例10: main
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
public static void main(String[] args) {
RestClient client = RestClient.forTarget("http://localhost:8080/api/");
try {
// Product PropertyBox
PropertyBox product = PropertyBox.builder(MProduct.PRODUCT).set(MProduct.DESCRIPTION, "Product 1")
.set(MProduct.SKU, "abc-123-xyz").set(MProduct.UNIT_PRICE, 9.99).build();
// add using POST
URI location = client.request().path("products").postForLocation(RequestEntity.json(product))
.orElseThrow(() -> new RuntimeException("Missing URI"));
System.out.println("Created URI: " + location);
// get the product
PropertyBox created = client.request().target(location).propertySet(MProduct.PRODUCT)
.getForEntity(PropertyBox.class).orElseThrow(() -> new RuntimeException("Missing product"));
System.out.println("Created id: " + created.getValue(MProduct.ID));
// update product
created.setValue(MProduct.DESCRIPTION, "Updated");
client.request().path("products/{id}").resolve("id", created.getValue(MProduct.ID))
.put(RequestEntity.json(created));
// read again
PropertyBox updated = client.request().path("products/{id}").resolve("id", created.getValue(MProduct.ID))
.propertySet(MProduct.PRODUCT).getForEntity(PropertyBox.class)
.orElseThrow(() -> new RuntimeException("Missing product"));
System.out.println("Updated description: " + updated.getValue(MProduct.DESCRIPTION));
// created another product
product = PropertyBox.builder(MProduct.PRODUCT).set(MProduct.DESCRIPTION, "Product 2")
.set(MProduct.SKU, "abc-456-xyz").set(MProduct.UNIT_PRICE, 19.99).build();
location = client.request().path("products").postForLocation(RequestEntity.json(product))
.orElseThrow(() -> new RuntimeException("Missing URI"));
System.out.println("Created URI: " + location);
// get all products as List
List<PropertyBox> values = client.request().path("products").propertySet(MProduct.PRODUCT)
.getAsList(PropertyBox.class);
System.out.println("Products: "
+ values.stream().map(pb -> pb.getValue(MProduct.ID) + " - " + pb.getValue(MProduct.DESCRIPTION))
.collect(Collectors.joining("; ")));
// delete al products
values.forEach(pb -> {
client.request().path("products/{id}").resolve("id", pb.getValue(MProduct.ID)).deleteOrFail();
System.out.println("Deleted product with id: " + pb.getValue(MProduct.ID));
});
// get all products again
values = client.request().path("products").propertySet(MProduct.PRODUCT).getAsList(PropertyBox.class);
// size should be 0 now
System.out.println("Products count: " + values.size());
} catch (UnsuccessfulResponseException e) {
// print exception message
System.err.println(e.getMessage());
// print the response body as String, if present
e.getResponse().as(String.class).ifPresent(r -> System.err.println(r));
}
}
示例11: testClient
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testClient() {
RestClient client = RestClient.forTarget("http://localhost:" + serverPort + "/api/");
// Product PropertyBox
PropertyBox product = PropertyBox.builder(PRODUCT).set(DESCRIPTION, "Product 1").set(SKU, "abc-123-xyz")
.set(UNIT_PRICE, 9.99).build();
// add using POST
URI location = client.request().path("products").postForLocation(RequestEntity.json(product))
.orElseThrow(() -> new RuntimeException("Missing URI"));
System.out.println("Created URI: " + location);
// get the product
PropertyBox created = client.request().target(location).propertySet(PRODUCT).getForEntity(PropertyBox.class)
.orElseThrow(() -> new RuntimeException("Missing product"));
System.out.println("Created id: " + created.getValue(ID));
// update product
created.setValue(DESCRIPTION, "Updated");
client.request().path("products/{id}").resolve("id", created.getValue(ID)).put(RequestEntity.json(created));
// read again
PropertyBox updated = client.request().path("products/{id}").resolve("id", created.getValue(ID))
.propertySet(PRODUCT).getForEntity(PropertyBox.class)
.orElseThrow(() -> new RuntimeException("Missing product"));
System.out.println("Updated description: " + updated.getValue(DESCRIPTION));
// created another product
product = PropertyBox.builder(PRODUCT).set(DESCRIPTION, "Product 2").set(SKU, "abc-456-xyz")
.set(UNIT_PRICE, 19.99).build();
location = client.request().path("products").postForLocation(RequestEntity.json(product))
.orElseThrow(() -> new RuntimeException("Missing URI"));
System.out.println("Created URI: " + location);
// get all products as List
List<PropertyBox> values = client.request().path("products").propertySet(PRODUCT).getAsList(PropertyBox.class);
System.out.println("Products: " + values.stream().map(pb -> pb.getValue(ID) + " - " + pb.getValue(DESCRIPTION))
.collect(Collectors.joining("; ")));
// delete al products
values.forEach(pb -> {
client.request().path("products/{id}").resolve("id", pb.getValue(ID)).deleteOrFail();
System.out.println("Deleted product with id: " + pb.getValue(ID));
});
// get all products again
values = client.request().path("products").propertySet(PRODUCT).getAsList(PropertyBox.class);
// size should be 0 now
System.out.println("Products count: " + values.size());
}
示例12: invoke
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Override
public <T, R> ResponseEntity<T> invoke(RequestDefinition requestDefinition, HttpMethod method,
RequestEntity<R> requestEntity, ResponseType<T> responseType, boolean onlySuccessfulStatusCode) {
// URI
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(requestDefinition.getRequestURI());
// query parameters
requestDefinition.getQueryParameters().forEach((n, v) -> builder.queryParam(n, v));
// template parameters
final String uri = builder.buildAndExpand(requestDefinition.getTemplateParameters()).toUriString();
// headers
HttpHeaders headers = new HttpHeaders();
requestDefinition.getHeaders().forEach((n, v) -> headers.add(n, v));
// Entity
HttpEntity<?> entity = new HttpEntity<>(getRequestPayload(requestEntity), headers);
// method
org.springframework.http.HttpMethod requestMethod = org.springframework.http.HttpMethod
.resolve(method.getMethodName());
if (requestMethod == null) {
throw new RestClientException("Unsupported HTTP method: " + method.getMethodName());
}
// get response, checking propertySet
final org.springframework.http.ResponseEntity<Resource> response;
if (requestDefinition.getPropertySet().isPresent()) {
response = requestDefinition.getPropertySet().get()
.execute(() -> invoke(uri, requestMethod, entity, responseType));
} else {
response = invoke(uri, requestMethod, entity, responseType);
}
// check error status code
int statusCode = response.getStatusCodeValue();
if (onlySuccessfulStatusCode && !HttpStatus.isSuccessStatusCode(statusCode)) {
throw new UnsuccessfulResponseException(new SpringResponseEntity<>(response, ResponseType.of(byte[].class),
getRestTemplate().getMessageConverters(), requestDefinition.getPropertySet().orElse(null)));
}
return new SpringResponseEntity<>(response, responseType, getRestTemplate().getMessageConverters(),
requestDefinition.getPropertySet().orElse(null));
}
示例13: testClient
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testClient() {
final RestClient client = SpringRestClient.create(restTemplate).defaultTarget(getBaseUri());
PropertyBox box = client.request().path("test").path("box/{id}").resolve("id", 1).propertySet(PROPERTIES)
.getForEntity(PropertyBox.class).orElse(null);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
HttpResponse<PropertyBox> rsp2 = client.request().path("test").path("box/{id}").resolve("id", 1)
.propertySet(PROPERTIES).get(PropertyBox.class);
assertEquals(HttpStatus.OK, rsp2.getStatus());
box = rsp2.getPayload().orElse(null);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
List<PropertyBox> boxes = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class);
assertNotNull(boxes);
assertEquals(2, boxes.size());
box = boxes.get(0);
assertNotNull(box);
assertEquals(new Integer(1), box.getValue(CODE));
assertEquals("value1", box.getValue(VALUE));
box = boxes.get(1);
assertNotNull(box);
assertEquals(new Integer(2), box.getValue(CODE));
assertEquals("value2", box.getValue(VALUE));
List<Integer> codes = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class).stream().map(p -> p.getValue(CODE)).collect(Collectors.toList());
assertNotNull(codes);
assertEquals(2, codes.size());
List<String> values = client.request().path("test").path("boxes").propertySet(PROPERTIES)
.getAsList(PropertyBox.class).stream().map(p -> p.getValue(VALUE)).collect(Collectors.toList());
assertNotNull(values);
assertEquals(2, values.size());
PropertyBox postBox = PropertyBox.builder(PROPERTIES).set(CODE, 100).set(VALUE, "post").build();
HttpResponse<Void> postResponse = client.request().path("test").path("box/post")
.post(RequestEntity.json(postBox));
assertEquals(HttpStatus.ACCEPTED, postResponse.getStatus());
}
示例14: testErrors
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Test
public void testErrors() {
final RestClient client = SpringRestClient.create(restTemplate).defaultTarget(getBaseUri());
ResponseEntity<TestData> r2 = client.request().path("test").path("data2/{id}").resolve("id", -1)
.get(TestData.class);
assertNotNull(r2);
assertEquals(HttpStatus.BAD_REQUEST, r2.getStatus());
ApiError error = r2.as(ApiError.class).orElse(null);
assertNotNull(error);
assertEquals("ERR000", error.getCode());
TestUtils.expectedException(UnsuccessfulResponseException.class, () -> {
client.request().path("test").path("data2/{id}").resolve("id", -1).getForEntity(TestData.class)
.orElse(null);
});
try {
client.request().path("test").path("data2/{id}").resolve("id", -1).getForEntity(TestData.class)
.orElse(null);
} catch (UnsuccessfulResponseException e) {
assertEquals(HttpStatus.BAD_REQUEST, e.getStatus().orElse(null));
assertNotNull(e.getResponse());
ApiError err = e.getResponse().as(ApiError.class).orElse(null);
assertNotNull(err);
assertEquals("ERR000", err.getCode());
}
ResponseEntity<?> rsp = client.request().path("test").path("data/save")
.put(RequestEntity.json(new TestData(-1, "testErr")));
assertNotNull(rsp);
assertEquals(HttpStatus.BAD_REQUEST, rsp.getStatus());
error = rsp.as(ApiError.class).orElse(null);
assertNotNull(error);
assertEquals("ERR000", error.getCode());
}
示例15: invoke
import com.holonplatform.http.rest.RequestEntity; //导入依赖的package包/类
@Override
public <T, R> ResponseEntity<T> invoke(HttpMethod method, RequestEntity<R> requestEntity,
ResponseType<T> responseType) {
return invoker.invoke(this, method, requestEntity, responseType, false);
}