Search in sources :

Example 21 with HttpEntity

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);
    });
}
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 22 with HttpEntity

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());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 23 with HttpEntity

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());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 24 with HttpEntity

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());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Test(org.junit.Test)

Example 25 with HttpEntity

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());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Test(org.junit.Test)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)85 HttpHeaders (org.springframework.http.HttpHeaders)62 Test (org.junit.Test)46 URI (java.net.URI)15 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 ArrayList (java.util.ArrayList)11 MediaType (org.springframework.http.MediaType)11 RestTemplate (org.springframework.web.client.RestTemplate)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 List (java.util.List)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5