use of org.springframework.http.ResponseEntity in project spring-framework by spring-projects.
the class RestTemplate method exchange.
@Override
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException {
Type type = responseType.getType();
RequestCallback requestCallback = httpEntityCallback(requestEntity, type);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type);
return execute(url, method, requestCallback, responseExtractor);
}
use of org.springframework.http.ResponseEntity in project spring-framework by spring-projects.
the class AsyncRestTemplateIntegrationTests method exchangePostCallbackWithLambdas.
@Test
public void exchangePostCallbackWithLambdas() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyHeader", "MyValue");
requestHeaders.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
final URI expected = new URI(baseUrl + "/post/1");
resultFuture.addCallback(result -> {
assertEquals("Invalid location", expected, result.getHeaders().getLocation());
assertFalse(result.hasBody());
}, ex -> fail(ex.getMessage()));
waitTillDone(resultFuture);
}
use of org.springframework.http.ResponseEntity in project spring-framework by spring-projects.
the class AsyncRestTemplateIntegrationTests method exchangeGetCallbackWithLambdas.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void exchangeGetCallbackWithLambdas() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyHeader", "MyValue");
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
ListenableFuture<ResponseEntity<String>> responseFuture = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
responseFuture.addCallback(result -> assertEquals("Invalid content", helloWorld, result.getBody()), ex -> fail(ex.getMessage()));
waitTillDone(responseFuture);
}
use of org.springframework.http.ResponseEntity in project spring-framework by spring-projects.
the class AsyncRestTemplateIntegrationTests method exchangePostCallback.
@Test
public void exchangePostCallback() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyHeader", "MyValue");
requestHeaders.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
final URI expected = new URI(baseUrl + "/post/1");
resultFuture.addCallback(new ListenableFutureCallback<ResponseEntity<Void>>() {
@Override
public void onSuccess(ResponseEntity<Void> result) {
assertEquals("Invalid location", expected, result.getHeaders().getLocation());
assertFalse(result.hasBody());
}
@Override
public void onFailure(Throwable ex) {
fail(ex.getMessage());
}
});
waitTillDone(resultFuture);
}
use of org.springframework.http.ResponseEntity in project spring-framework by spring-projects.
the class ResponseEntityResultHandler method handleResult.
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Mono<?> returnValueMono;
MethodParameter bodyParameter;
ReactiveAdapter adapter = getAdapter(result);
if (adapter != null) {
Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
bodyParameter = result.getReturnTypeSource().nested().nested();
} else {
returnValueMono = Mono.justOrEmpty(result.getReturnValue());
bodyParameter = result.getReturnTypeSource().nested();
}
return returnValueMono.then(returnValue -> {
Assert.isInstanceOf(HttpEntity.class, returnValue, "HttpEntity expected");
HttpEntity<?> httpEntity = (HttpEntity<?>) returnValue;
if (httpEntity instanceof ResponseEntity) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
}
HttpHeaders entityHeaders = httpEntity.getHeaders();
HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
if (!entityHeaders.isEmpty()) {
entityHeaders.entrySet().stream().filter(entry -> !responseHeaders.containsKey(entry.getKey())).forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
}
if (httpEntity.getBody() == null) {
return exchange.getResponse().setComplete();
}
String etag = entityHeaders.getETag();
Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
HttpMethod httpMethod = exchange.getRequest().getMethod();
if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
return exchange.getResponse().setComplete();
}
return writeBody(httpEntity.getBody(), bodyParameter, exchange);
});
}
Aggregations