Search in sources :

Example 56 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class DefaultServerRequestTests method bodyToMonoParameterizedTypeReference.

@Test
public void bodyToMonoParameterizedTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com?foo=bar").headers(httpHeaders).body(body);
    DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);
    ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<>() {
    };
    Mono<String> resultMono = request.bodyToMono(typeReference);
    assertThat(resultMono.block()).isEqualTo("foo");
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 57 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class DefaultServerRequestTests method bodyToFluxParameterizedTypeReference.

@Test
public void bodyToFluxParameterizedTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com?foo=bar").headers(httpHeaders).body(body);
    DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);
    ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<>() {
    };
    Flux<String> resultFlux = request.bodyToFlux(typeReference);
    assertThat(resultFlux.collectList().block()).isEqualTo(Collections.singletonList("foo"));
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 58 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new LinkedMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.add("foo", "bar");
    HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
    builder.part("entity", entity).header("baz", "qux");
    Publisher<String> publisher = Flux.just("foo", "bar", "baz");
    builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux");
    builder.asyncPart("publisherPtr", publisher, new ParameterizedTypeReference<String>() {
    }).header("baz", "qux");
    MultiValueMap<String, HttpEntity<?>> result = builder.build();
    assertThat(result.size()).isEqualTo(5);
    HttpEntity<?> resultEntity = result.getFirst("key");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(multipartData);
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    resultEntity = result.getFirst("logo");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(logo);
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("entity");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo("body");
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherClass");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.forClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherPtr");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.forClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) PublisherEntity(org.springframework.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Example 59 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class RequestEntityTests method types.

// SPR-13154
@Test
void types() throws URISyntaxException {
    URI url = new URI("https://example.com");
    List<String> body = Arrays.asList("foo", "bar");
    ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<String>>() {
    };
    RequestEntity<?> entity = RequestEntity.post(url).body(body, typeReference.getType());
    assertThat(entity.getType()).isEqualTo(typeReference.getType());
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 60 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
 * {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementTypeRef the type of values to be produced
 * @return the inserter to write a producer
 * @since 5.2
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, ParameterizedTypeReference<?> elementTypeRef) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementTypeRef, "'elementTypeRef' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.forType(elementTypeRef), adapter);
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Collectors(java.util.stream.Collectors) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)62 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)28 List (java.util.List)22 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 HttpEntity (org.springframework.http.HttpEntity)10 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)8 URISyntaxException (java.net.URISyntaxException)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 HttpMethod (org.springframework.http.HttpMethod)4