Using Gson instead of Jackson in Spring Webflux

Viewed 1416

We have many Spring MVC projects already, which all use gson instead of jackson for response body encode. Our bean classes are all written based on gson annotation. Now I am setting up a Spring Webflux restful server. It would save a lot of work if we can use the old bean classes from our Spring MVC projects.

I have tried spring.http.converters.preferred-json-mapper=gson property to no avail.

I have tried HttpMessageConverter bean, which is included in webflux packages, but that does not work as in the Spring MVC projects.

I googled a lot and the only thing helpful is to implement org.springframework.http.codec.HttpMessageEncoder class and set it to WebFluxConfigurer.configureHttpMessageCodecs() method:

@Configuration
public class WebConfiguration implements WebFluxConfigurer {
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.customCodecs().decoder(new GsonHttpMessageDecoder());
        configurer.customCodecs().encoder(new GsonHttpMessageEncoder());
    }

    private static class GsonHttpMessageEncoder implements HttpMessageEncoder {
        ...
    }

    private static class GsonHttpMessageDecoder implements HttpMessageDecoder {
        ...
    }
}

I haven't try this out yet, since it is a little complex. Is there some easy way to replace jackson with gson in Spring Webflux?

Any help is appreciated.

1 Answers

Spring Framework doesn't support GSON as a WebFlux Encoder / Decoder for now. Feel free to follow up on the dedicated issue.

Note that as far as I know, GSON doesn't support non-blocking parsing so even if the support is implemented in Framework, it won't be complete and should not cover streaming input use cases.

Related