use of org.springframework.http.HttpEntity 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);
});
}
use of org.springframework.http.HttpEntity in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method resolveGenericArgument.
@Test
public void resolveGenericArgument() throws Exception {
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
@SuppressWarnings("unchecked") HttpEntity<List<SimpleBean>> result = (HttpEntity<List<SimpleBean>>) processor.resolveArgument(paramList, mavContainer, webRequest, binderFactory);
assertNotNull(result);
assertEquals("Jad", result.getBody().get(0).getName());
assertEquals("Robert", result.getBody().get(1).getName());
}
use of org.springframework.http.HttpEntity in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method resolveArgumentTypeVariable.
@Test
public void resolveArgumentTypeVariable() throws Exception {
Method method = MySimpleParameterizedController.class.getMethod("handleDto", HttpEntity.class);
HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
@SuppressWarnings("unchecked") HttpEntity<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(methodParam, mavContainer, webRequest, binderFactory);
assertNotNull(result);
assertEquals("Jad", result.getBody().getName());
}
use of org.springframework.http.HttpEntity in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method resolveArgument.
@Test
public void resolveArgument() throws Exception {
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
@SuppressWarnings("unchecked") HttpEntity<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(paramSimpleBean, mavContainer, webRequest, binderFactory);
assertNotNull(result);
assertEquals("Jad", result.getBody().getName());
}
use of org.springframework.http.HttpEntity in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method resolveArgumentWithEmptyBody.
// SPR-12861
@Test
public void resolveArgumentWithEmptyBody() throws Exception {
this.servletRequest.setContent(new byte[0]);
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
HttpEntity<?> result = (HttpEntity<?>) processor.resolveArgument(this.paramSimpleBean, this.mavContainer, this.webRequest, this.binderFactory);
assertNotNull(result);
assertNull(result.getBody());
}
Aggregations