use of org.springframework.core.MethodParameter 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.core.MethodParameter in project spring-framework by spring-projects.
the class MessageReaderArgumentResolverTests method emptyBody.
// More extensive "empty body" tests in RequestBody- and HttpEntityArgumentResolverTests
// SPR-9942
@Test
// SPR-9942
@SuppressWarnings("unchecked")
public void emptyBody() throws Exception {
ServerWebExchange exchange = post("/path").contentType(MediaType.APPLICATION_JSON).toExchange();
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
MethodParameter param = this.testMethod.arg(type);
Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(param, true, this.bindingContext, exchange).block();
StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.
the class MessageReaderArgumentResolverTests method observableTestBean.
@Test
public void observableTestBean() throws Exception {
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
ResolvableType type = forClassWithGenerics(Observable.class, TestBean.class);
MethodParameter param = this.testMethod.arg(type);
Observable<?> observable = resolveValue(param, body);
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), observable.toList().toBlocking().first());
}
use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.
the class MessageReaderArgumentResolverTests method list.
@Test
public void list() throws Exception {
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
ResolvableType type = forClassWithGenerics(List.class, TestBean.class);
MethodParameter param = this.testMethod.arg(type);
List<?> list = resolveValue(param, body);
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), list);
}
use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.
the class MessageReaderArgumentResolverTests method parameterizedMethodArgument.
// SPR-9964
@Test
public void parameterizedMethodArgument() throws Exception {
Method method = AbstractParameterizedController.class.getMethod("handleDto", Identifiable.class);
HandlerMethod handlerMethod = new HandlerMethod(new ConcreteParameterizedController(), method);
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
SimpleBean simpleBean = resolveValue(methodParam, "{\"name\" : \"Jad\"}");
assertEquals("Jad", simpleBean.getName());
}
Aggregations