6

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.

Lyux
  • 431
  • 6
  • 12
  • Not sure if this will work, but have you tried excluding the transitive Jackson dependency? That might force WebFlux to use the configuration you've provided (GSON). – Mario Ishac Oct 15 '19 at 05:47
  • why would one use gson over jackson? – Toerktumlare Oct 15 '19 at 06:07
  • @ThomasAndolf Because we already have many beans written with gson annotation like `@Expose` `@SerializedName` in SpringMVC projects, and these beans are shared across our projects by a jar as a common api definition between services. If we use jackson, we must rewrite these beans and logic along with them. I think it is not a good practice to have two code implementation represent one fact, since it violates Single-Point-of-Truth rule. Apart from that, no big deal indeed. If solving the problem cost too much, I will surrender and use jackson~. – Lyux Oct 15 '19 at 06:42
  • looks like WebConfiguration is the only way to go. webflux does not support gson out of box. – ozkanpakdil Oct 19 '19 at 09:21
  • @ThomasAndolf - One reason would be that Gson is a better platform for highly customized serialization of generic types. Jackson is battle-hardened and widely-used, but its design is incoherent when it comes to handling generic types. – Doradus Aug 30 '20 at 14:56
  • @Dorados which is a highly subjective opinion – Toerktumlare Aug 30 '20 at 19:49

1 Answers1

2

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.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176