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");
}
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"));
}
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");
}
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());
}
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);
}
Aggregations