Search in sources :

Example 1 with ServerCodecConfigurer

use of org.springframework.http.codec.ServerCodecConfigurer in project spring-framework by spring-projects.

the class ServerCodecConfigurerTests method cloneConfigurer.

@Test
public void cloneConfigurer() {
    ServerCodecConfigurer clone = this.configurer.clone();
    MultipartHttpMessageReader reader = new MultipartHttpMessageReader(new DefaultPartHttpMessageReader());
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    clone.defaultCodecs().multipartReader(reader);
    clone.defaultCodecs().serverSentEventEncoder(encoder);
    // Clone has the customizations
    HttpMessageReader<?> actualReader = findCodec(clone.getReaders(), MultipartHttpMessageReader.class);
    ServerSentEventHttpMessageWriter actualWriter = findCodec(clone.getWriters(), ServerSentEventHttpMessageWriter.class);
    assertThat(actualReader).isSameAs(reader);
    assertThat(actualWriter.getEncoder()).isSameAs(encoder);
    // Original does not have the customizations
    actualReader = findCodec(this.configurer.getReaders(), MultipartHttpMessageReader.class);
    actualWriter = findCodec(this.configurer.getWriters(), ServerSentEventHttpMessageWriter.class);
    assertThat(actualReader).isNotSameAs(reader);
    assertThat(actualWriter.getEncoder()).isNotSameAs(encoder);
}
Also used : MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 2 with ServerCodecConfigurer

use of org.springframework.http.codec.ServerCodecConfigurer in project spring-framework by spring-projects.

the class ControllerMethodResolverTests method setup.

@BeforeEach
public void setup() {
    ArgumentResolverConfigurer resolvers = new ArgumentResolverConfigurer();
    resolvers.addCustomResolver(new CustomArgumentResolver());
    resolvers.addCustomResolver(new CustomSyncArgumentResolver());
    ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
    codecs.customCodecs().register(new ByteArrayDecoder());
    codecs.customCodecs().register(new ByteBufferDecoder());
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.registerBean(TestControllerAdvice.class);
    applicationContext.refresh();
    this.methodResolver = new ControllerMethodResolver(resolvers, ReactiveAdapterRegistry.getSharedInstance(), applicationContext, codecs.getReaders());
    Method method = ResolvableMethod.on(TestController.class).mockCall(TestController::handle).method();
    this.handlerMethod = new HandlerMethod(new TestController(), method);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ServerCodecConfigurer

use of org.springframework.http.codec.ServerCodecConfigurer in project spring-framework by spring-projects.

the class DelegatingWebFluxConfigurationTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() {
    delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
    ReactiveAdapterRegistry reactiveAdapterRegistry = delegatingConfig.webFluxAdapterRegistry();
    ServerCodecConfigurer serverCodecConfigurer = delegatingConfig.serverCodecConfigurer();
    FormattingConversionService formattingConversionService = delegatingConfig.webFluxConversionService();
    Validator validator = delegatingConfig.webFluxValidator();
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) this.delegatingConfig.requestMappingHandlerAdapter(reactiveAdapterRegistry, serverCodecConfigurer, formattingConversionService, validator).getWebBindingInitializer();
    verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
    verify(webFluxConfigurer).getValidator();
    verify(webFluxConfigurer).getMessageCodesResolver();
    verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
    verify(webFluxConfigurer).configureArgumentResolvers(any());
    assertThat(initializer).isNotNull();
    boolean condition = initializer.getValidator() instanceof LocalValidatorFactoryBean;
    assertThat(condition).isTrue();
    assertThat(initializer.getConversionService()).isSameAs(formatterRegistry.getValue());
    assertThat(codecsConfigurer.getValue().getReaders().size()).isEqualTo(14);
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 4 with ServerCodecConfigurer

use of org.springframework.http.codec.ServerCodecConfigurer in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Assert.notNull(this.applicationContext, "ApplicationContext is required");
    if (CollectionUtils.isEmpty(this.messageReaders)) {
        ServerCodecConfigurer codecConfigurer = ServerCodecConfigurer.create();
        this.messageReaders = codecConfigurer.getReaders();
    }
    if (this.argumentResolverConfigurer == null) {
        this.argumentResolverConfigurer = new ArgumentResolverConfigurer();
    }
    if (this.reactiveAdapterRegistry == null) {
        this.reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
    }
    this.methodResolver = new ControllerMethodResolver(this.argumentResolverConfigurer, this.reactiveAdapterRegistry, this.applicationContext, this.messageReaders);
    this.modelInitializer = new ModelInitializer(this.methodResolver, this.reactiveAdapterRegistry);
}
Also used : ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer)

Example 5 with ServerCodecConfigurer

use of org.springframework.http.codec.ServerCodecConfigurer in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method serverCodecConfigurer.

/**
 * Return the configurer for HTTP message readers and writers.
 * <p>Use {@link #configureHttpMessageCodecs(ServerCodecConfigurer)} to
 * configure the readers and writers.
 */
@Bean
public ServerCodecConfigurer serverCodecConfigurer() {
    ServerCodecConfigurer serverCodecConfigurer = ServerCodecConfigurer.create();
    configureHttpMessageCodecs(serverCodecConfigurer);
    return serverCodecConfigurer;
}
Also used : ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) Bean(org.springframework.context.annotation.Bean)

Aggregations

ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)6 Test (org.junit.jupiter.api.Test)2 Method (java.lang.reflect.Method)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)1 ByteArrayDecoder (org.springframework.core.codec.ByteArrayDecoder)1 ByteBufferDecoder (org.springframework.core.codec.ByteBufferDecoder)1 FormattingConversionService (org.springframework.format.support.FormattingConversionService)1 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)1 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)1 DefaultPartHttpMessageReader (org.springframework.http.codec.multipart.DefaultPartHttpMessageReader)1 MultipartHttpMessageReader (org.springframework.http.codec.multipart.MultipartHttpMessageReader)1 Validator (org.springframework.validation.Validator)1 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)1 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1 InvocableHandlerMethod (org.springframework.web.reactive.result.method.InvocableHandlerMethod)1 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)1