Search in sources :

Example 66 with ResolvableType

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

the class JsonComponentModule method addSerializerWithDeducedType.

@SuppressWarnings({ "unchecked" })
private <T> void addSerializerWithDeducedType(JsonSerializer<T> serializer) {
    ResolvableType type = ResolvableType.forClass(JsonSerializer.class, serializer.getClass());
    addSerializer((Class<T>) type.resolveGeneric(), serializer);
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

Example 67 with ResolvableType

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

the class NoSuchBeanDefinitionFailureAnalyzer method extractBeanType.

private Class<?> extractBeanType(ResolvableType resolvableType) {
    ResolvableType collectionType = resolvableType.asCollection();
    if (!collectionType.equals(ResolvableType.NONE)) {
        return collectionType.getGeneric(0).getRawClass();
    }
    ResolvableType mapType = resolvableType.asMap();
    if (!mapType.equals(ResolvableType.NONE)) {
        return mapType.getGeneric(1).getRawClass();
    }
    return resolvableType.getRawClass();
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

Example 68 with ResolvableType

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

the class WebFluxConfigurationSupportTests method assertHasMessageReader.

private void assertHasMessageReader(List<HttpMessageReader<?>> readers, Class<?> clazz, MediaType mediaType) {
    ResolvableType type = ResolvableType.forClass(clazz);
    assertTrue(readers.stream().anyMatch(c -> mediaType == null || c.canRead(type, mediaType)));
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Validator(org.springframework.validation.Validator) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebHandler(org.springframework.web.server.WebHandler) ByteBuffer(java.nio.ByteBuffer) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) MimeType(org.springframework.util.MimeType) FreeMarkerConfigurer(org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer) APPLICATION_OCTET_STREAM(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM) HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) View(org.springframework.web.reactive.result.view.View) ResolvableType(org.springframework.core.ResolvableType) AbstractHandlerMapping(org.springframework.web.reactive.handler.AbstractHandlerMapping) Resource(org.springframework.core.io.Resource) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Ordered(org.springframework.core.Ordered) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConversionService(org.springframework.core.convert.ConversionService) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) ViewResolutionResultHandler(org.springframework.web.reactive.result.view.ViewResolutionResultHandler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) TEXT_PLAIN(org.springframework.http.MediaType.TEXT_PLAIN) Test(org.junit.Test) MimeTypeUtils(org.springframework.util.MimeTypeUtils) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ApplicationContext(org.springframework.context.ApplicationContext) ResponseEntityResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) IMAGE_PNG(org.springframework.http.MediaType.IMAGE_PNG) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) Bean(org.springframework.context.annotation.Bean) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert.assertEquals(org.junit.Assert.assertEquals) ResolvableType(org.springframework.core.ResolvableType)

Example 69 with ResolvableType

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

the class WebFluxConfigurationSupportTests method assertHasMessageWriter.

private void assertHasMessageWriter(List<HttpMessageWriter<?>> writers, Class<?> clazz, MediaType mediaType) {
    ResolvableType type = ResolvableType.forClass(clazz);
    assertTrue(writers.stream().anyMatch(c -> mediaType == null || c.canWrite(type, mediaType)));
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Validator(org.springframework.validation.Validator) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebHandler(org.springframework.web.server.WebHandler) ByteBuffer(java.nio.ByteBuffer) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) MimeType(org.springframework.util.MimeType) FreeMarkerConfigurer(org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer) APPLICATION_OCTET_STREAM(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM) HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) View(org.springframework.web.reactive.result.view.View) ResolvableType(org.springframework.core.ResolvableType) AbstractHandlerMapping(org.springframework.web.reactive.handler.AbstractHandlerMapping) Resource(org.springframework.core.io.Resource) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Ordered(org.springframework.core.Ordered) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConversionService(org.springframework.core.convert.ConversionService) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) ViewResolutionResultHandler(org.springframework.web.reactive.result.view.ViewResolutionResultHandler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) TEXT_PLAIN(org.springframework.http.MediaType.TEXT_PLAIN) Test(org.junit.Test) MimeTypeUtils(org.springframework.util.MimeTypeUtils) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ApplicationContext(org.springframework.context.ApplicationContext) ResponseEntityResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) IMAGE_PNG(org.springframework.http.MediaType.IMAGE_PNG) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) Bean(org.springframework.context.annotation.Bean) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert.assertEquals(org.junit.Assert.assertEquals) ResolvableType(org.springframework.core.ResolvableType)

Example 70 with ResolvableType

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

the class SseIntegrationTests method sseAsEvent.

@Test
public void sseAsEvent() throws Exception {
    ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);
    Flux<ServerSentEvent<String>> result = this.webClient.get().uri("/event").accept(TEXT_EVENT_STREAM).exchange().flatMap(response -> response.body(toFlux(type)));
    StepVerifier.create(result).consumeNextWith(event -> {
        assertEquals("0", event.id().get());
        assertEquals("foo", event.data().get());
        assertEquals("bar", event.comment().get());
        assertFalse(event.event().isPresent());
        assertFalse(event.retry().isPresent());
    }).consumeNextWith(event -> {
        assertEquals("1", event.id().get());
        assertEquals("foo", event.data().get());
        assertEquals("bar", event.comment().get());
        assertFalse(event.event().isPresent());
        assertFalse(event.retry().isPresent());
    }).thenCancel().verify(Duration.ofSeconds(5L));
}
Also used : StepVerifier(reactor.test.StepVerifier) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) BodyExtractors.toFlux(org.springframework.web.reactive.function.BodyExtractors.toFlux) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.Test) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) RestController(org.springframework.web.bind.annotation.RestController) TEXT_EVENT_STREAM(org.springframework.http.MediaType.TEXT_EVENT_STREAM) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) HttpHandler(org.springframework.http.server.reactive.HttpHandler) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) Assert.assertFalse(org.junit.Assert.assertFalse) Duration(java.time.Duration) AbstractHttpHandlerIntegrationTests(org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) Bean(org.springframework.context.annotation.Bean) ResolvableType(org.springframework.core.ResolvableType) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

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