Search code examples
spring-bootspring-webspring-webflux

Is it possible to add your own text/event-stream Serializer to Spring Boot 2.0.0?


Currently the only serialization of text/event-stream is done via ServerSentEventHttpMessageWriter. Is it possible to provide your own writer to change the way the data is serialized (providing a different format besides the SSE format) ?


Solution

  • You can add custom codecs and message readers/writers with a webflux @Configuration class; add something like this to your Spring Boot application:

    @Configuration
    public class MyConfiguration implements WebFluxConfigurer {
    
        @Override
        public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
            configurer.customCodecs().writer(customHttpMessageWriter);
        }
    }
    

    The javadoc for ServerCodecConfigurer, CodecConfigurer (and its inner interfaces) should be useful as well.