Search in sources :

Example 46 with ResolvableType

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

the class TypeDescriptor method map.

/**
	 * Create a new type descriptor from a {@link java.util.Map} type.
	 * <p>Useful for converting to typed Maps.
	 * <p>For example, a Map&lt;String, String&gt; could be converted to a Map&lt;Id, EmailAddress&gt;
	 * by converting to a targetType built with this method:
	 * The method call to construct such a TypeDescriptor would look something like:
	 * <pre class="code">
	 * map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
	 * </pre>
	 * @param mapType the map type, which must implement {@link Map}
	 * @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys
	 * @param valueTypeDescriptor the map's value type, used to convert map values
	 * @return the map type descriptor
	 */
public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescriptor, TypeDescriptor valueTypeDescriptor) {
    Assert.notNull(mapType, "Map type must not be null");
    if (!Map.class.isAssignableFrom(mapType)) {
        throw new IllegalArgumentException("Map type must be a [java.util.Map]");
    }
    ResolvableType key = (keyTypeDescriptor != null ? keyTypeDescriptor.resolvableType : null);
    ResolvableType value = (valueTypeDescriptor != null ? valueTypeDescriptor.resolvableType : null);
    return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with ResolvableType

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

the class ApplicationListenerMethodAdapter method getResolvableType.

private ResolvableType getResolvableType(ApplicationEvent event) {
    ResolvableType payloadType = null;
    if (event instanceof PayloadApplicationEvent) {
        PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
        payloadType = payloadEvent.getResolvableType().as(PayloadApplicationEvent.class).getGeneric();
    }
    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) && payloadType != null) {
            if (declaredEventType.isAssignableFrom(payloadType)) {
                return declaredEventType;
            }
        }
        if (declaredEventType.getRawClass().isInstance(event)) {
            return declaredEventType;
        }
    }
    return null;
}
Also used : PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ResolvableType(org.springframework.core.ResolvableType)

Example 48 with ResolvableType

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

the class Jackson2JsonDecoderTests method canDecode.

@Test
public void canDecode() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    ResolvableType type = ResolvableType.forClass(Pojo.class);
    assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
    assertTrue(decoder.canDecode(type, null));
    assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 49 with ResolvableType

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

the class Jackson2JsonDecoderTests method jsonView.

@Test
public void jsonView() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
    ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
    Map<String, Object> hints = Collections.singletonMap(Jackson2JsonDecoder.JSON_VIEW_HINT, MyJacksonView1.class);
    Flux<JacksonViewBean> flux = new Jackson2JsonDecoder().decode(source, elementType, null, hints).cast(JacksonViewBean.class);
    StepVerifier.create(flux).consumeNextWith(b -> {
        assertTrue(b.getWithView1().equals("with"));
        assertNull(b.getWithView2());
        assertNull(b.getWithoutView());
    }).expectComplete().verify();
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 50 with ResolvableType

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

the class ResourceHttpMessageWriterTests method testWrite.

private void testWrite(MockServerHttpRequest request) {
    Mono<Resource> input = Mono.just(this.resource);
    ResolvableType type = ResolvableType.forClass(Resource.class);
    MediaType contentType = MediaType.TEXT_PLAIN;
    Map<String, Object> hints = Collections.emptyMap();
    Mono<Void> mono = this.writer.write(input, null, type, contentType, request, this.response, hints);
    StepVerifier.create(mono).expectNextCount(0).expectComplete().verify();
}
Also used : ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType)

Aggregations

ResolvableType (org.springframework.core.ResolvableType)96 Test (org.junit.Test)66 MethodParameter (org.springframework.core.MethodParameter)20 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)14 List (java.util.List)11 MediaType (org.springframework.http.MediaType)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Map (java.util.Map)9 Assert (org.springframework.util.Assert)7 Collections (java.util.Collections)6 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)6 ArrayList (java.util.ArrayList)5 ReactiveAdapter (org.springframework.core.ReactiveAdapter)5 Pojo (org.springframework.http.codec.Pojo)5 ServerWebInputException (org.springframework.web.server.ServerWebInputException)5 Publisher (org.reactivestreams.Publisher)4 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)4 Single (rx.Single)4