I've already created question on stackoverflow, but it seems as bug, not configuration issue.

Versions: Spring boot: 2.5.3 Gson: 2.8.7 Java version: 16.0.1.9

The tagret is apply custom object mapping using Gson as main json library (jackson dependency was removed, and gson was set in application.properties) I have tried: - Adding Gson/GsonBuilder bean, where calling GsonBuilder#registerTypeAdapter - Overriding WebMvcConfigurer#extendMessageConverters, replacing existing GsonHttpMessageConverter with new one with my gson.

All other gson settings as pretty-printing, serializing-nulls, GsonBuilder#addSerializationExclusionStrategy and GsonBuilder#addDeserializationExclusionStrategy working fine, but typeadapters not.

@Configuration
public class WebMVCConfig implements WebMvcConfigurer  {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(c -> c instanceof GsonHttpMessageConverter);
        converters.add(new GsonHttpMessageConverter(gson()));
    }

    @Bean
    public GsonBuilder gsonBuilder() {
        var strategy = new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getAnnotation(GsonExclude.class) != null;
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        };
        return new GsonBuilder()
            .registerTypeAdapter(Version.class, new VersionGsonAdapter())
            .addSerializationExclusionStrategy(strategy)
            .addDeserializationExclusionStrategy(strategy)
            .disableHtmlEscaping()
            .serializeNulls()
            .setPrettyPrinting();
    }

    @Bean
    public Gson gson() {
        return gsonBuilder().create();
    }
}

As I found out using debbuger, Spring trying to create new instance of Gson every time I making request to my RestController, even if I will autowire gson instance, it will send wrong one insteadof my bean Gson.

Comment From: bclozel

Thanks for getting in touch, but it feels like this is a question that would be better suited to StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

I've added an answer to your StackOverflow question - let's continue the discussion there.