Search in sources :

Example 71 with MethodParameter

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);
    });
}
Also used : RequestEntity(org.springframework.http.RequestEntity) Arrays(java.util.Arrays) ReactiveAdapter(org.springframework.core.ReactiveAdapter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResponseEntity(org.springframework.http.ResponseEntity) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) Instant(java.time.Instant) MethodParameter(org.springframework.core.MethodParameter) ReactiveAdapter(org.springframework.core.ReactiveAdapter) HttpMethod(org.springframework.http.HttpMethod)

Example 72 with MethodParameter

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();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerWebInputException(org.springframework.web.server.ServerWebInputException) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 73 with MethodParameter

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());
}
Also used : ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 74 with MethodParameter

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 75 with MethodParameter

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());
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.method.ResolvableMethod) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19