/** * Return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference}. * <p>Note: The resulting {@link ResolvableType} instance may not be {@link Serializable}. * @param typeReference the reference to obtain the source type from * @return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference} * @since 4.3.12 * @see #forType(Type) */ public static ResolvableType forType(ParameterizedTypeReference<?> typeReference) { return forType(typeReference.getType(), null, null); }
/** * Variant of {@link #toMono(Class)} for type information with generics. * @param typeRef the type reference for the type to decode to * @param <T> the element type to decode to * @return {@code BodyExtractor} for {@code Mono<T>} */ public static <T> BodyExtractor<Mono<T>, ReactiveHttpInputMessage> toMono(ParameterizedTypeReference<T> typeRef) { return toMono(ResolvableType.forType(typeRef.getType())); }
/** * Inserter to write the given {@link Publisher}. * <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 publisher the publisher to write with * @param typeReference the type of elements contained in the publisher * @param <T> the type of the elements contained in the publisher * @param <P> the {@code Publisher} type * @return the inserter to write a {@code Publisher} */ public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMessage> fromPublisher( P publisher, ParameterizedTypeReference<T> typeReference) { Assert.notNull(publisher, "Publisher must not be null"); Assert.notNull(typeReference, "ParameterizedTypeReference must not be null"); return (message, context) -> writeWithMessageWriters(message, context, publisher, ResolvableType.forType(typeReference.getType())); }
/** * Variant of {@link #toFlux(Class)} for type information with generics. * @param typeRef the type reference for the type to decode to * @param <T> the element type to decode to * @return {@code BodyExtractor} for {@code Flux<T>} */ public static <T> BodyExtractor<Flux<T>, ReactiveHttpInputMessage> toFlux(ParameterizedTypeReference<T> typeRef) { return toFlux(ResolvableType.forType(typeRef.getType())); }
@Override public <T> ListenableFuture<ResponseEntity<T>> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException { Type type = responseType.getType(); AsyncRequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return execute(url, method, requestCallback, responseExtractor, uriVariables); }
@Override public <T> ListenableFuture<ResponseEntity<T>> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException { Type type = responseType.getType(); AsyncRequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return execute(url, method, requestCallback, responseExtractor, uriVariables); }
@Override public <T> ListenableFuture<ResponseEntity<T>> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException { Type type = responseType.getType(); AsyncRequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return execute(url, method, requestCallback, responseExtractor); }
@Override public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException { Type type = responseType.getType(); RequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return nonNull(execute(url, method, requestCallback, responseExtractor, uriVariables)); }
@Override public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException { Type type = responseType.getType(); RequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return nonNull(execute(url, method, requestCallback, responseExtractor, uriVariables)); }
@Override public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException { Type type = responseType.getType(); RequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return nonNull(execute(url, method, requestCallback, responseExtractor)); }
@Test public void listTypeReference() throws Exception { Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {}; assertEquals(listType, typeReference.getType()); }
@Test public void mapTypeReference() throws Exception { Type mapType = getClass().getMethod("mapMethod").getGenericReturnType(); ParameterizedTypeReference<Map<Object,String>> typeReference = new ParameterizedTypeReference<Map<Object,String>>() {}; assertEquals(mapType, typeReference.getType()); }
@Test public void reflectiveTypeReferenceWithSpecificDeclaration() throws Exception{ Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<List<String>> typeReference = ParameterizedTypeReference.forType(listType); assertEquals(listType, typeReference.getType()); }
@Test public void reflectiveTypeReferenceWithGenericDeclaration() throws Exception{ Type listType = getClass().getMethod("listMethod").getGenericReturnType(); ParameterizedTypeReference<?> typeReference = ParameterizedTypeReference.forType(listType); assertEquals(listType, typeReference.getType()); }
@Test public void stringTypeReference() { ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {}; assertEquals(String.class, typeReference.getType()); }
@Test // SPR-13154 public void types() throws URISyntaxException { URI url = new URI("http://example.com"); List<String> body = Arrays.asList("foo", "bar"); ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<String>>() {}; RequestEntity<?> entity = RequestEntity.post(url).body(body, typeReference.getType()); assertEquals(typeReference.getType(), entity.getType()); }
@Override public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException { Type type = responseType.getType(); RequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return nonNull(doExecute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor)); }
@Test // SPR-13154 public void jsonPostForObjectWithJacksonTypeInfoList() throws URISyntaxException { List<ParentClass> list = new ArrayList<>(); list.add(new Foo("foo")); list.add(new Bar("bar")); ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<ParentClass>>() {}; RequestEntity<List<ParentClass>> entity = RequestEntity .post(new URI(baseUrl + "/jsonpost")) .contentType(new MediaType("application", "json", StandardCharsets.UTF_8)) .body(list, typeReference.getType()); String content = template.exchange(entity, String.class).getBody(); assertTrue(content.contains("\"type\":\"foo\"")); assertTrue(content.contains("\"type\":\"bar\"")); }
@Test @SuppressWarnings("unchecked") public void writeParameterizedBaseType() throws Exception { ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {}; ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {}; String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage); assertEquals(1, results.size()); MyBean result = results.get(0); assertEquals("Foo", result.getString()); assertEquals(42, result.getNumber()); assertEquals(42F, result.getFraction(), 0F); assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray()); assertTrue(result.isBool()); assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes()); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); }
@Test @SuppressWarnings("unchecked") public void generics() throws IOException { GenericHttpMessageConverter<String> converter = mock(GenericHttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); String expected = "Foo"; ParameterizedTypeReference<List<String>> reference = new ParameterizedTypeReference<List<String>>() {}; Type type = reference.getType(); extractor = new HttpMessageConverterExtractor<List<String>>(type, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes())); given(converter.canRead(type, null, contentType)).willReturn(true); given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected); Object result = extractor.extractData(response); assertEquals(expected, result); }