强制 Spring-Boot 使用 Gson 而不是 Jackson [英] Force Spring-Boot to use Gson instead of Jackson

查看:40
本文介绍了强制 Spring-Boot 使用 Gson 而不是 Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring-Boot 1.4.2 参考声明:

Spring-Boot 1.4.2 reference claims:

spring.http.converters.preferred-json-mapper=jackson # 用于 HTTP 消息转换的首选 JSON 映射器.设置为"gson"强制使用Gson

spring.http.converters.preferred-json-mapper=jackson # Preferred JSON mapper to use for HTTP message conversion. Set to "gson" to force the use of Gson

我们做到了.

  • 我们为 preferred-json-mapper 设置了 gson.
  • 我们已添加 Gson 作为我们项目的依赖项.

但杰克逊仍然被使用.

最后,在排除了 Maven 中指向 Jackson 的所有传递依赖项后,我们设法强制 Spring-Boot 使用 Gson.

Finally we manage to force Spring-Boot to use Gson after exclude all transitive dependencies in Maven which have been pointing out to Jackson.

现在的问题是.这是强制 Spring-Boot 使用 Gson 而不是 Jackson 的唯一方法吗?我们真的需要排除所有指向 Jackson 的传递依赖吗?优选-json-mapper 设置不够?

Now the question is. Is it the only way to force Spring-Boot to use Gson instead of Jackson? Do we really need to exclude all transitive dependencies pointing out to Jackson? The preferred-json-mapper setting is not enough?

推荐答案

发生在 Spring Boot 使用的旧 *.xml 配置的一部分中,我们有 <mvc:annotation-driven/>.

It occurred that in part of our old *.xml config which was used by Spring Boot we had <mvc:annotation-driven/>.

这导致第二次使用没有 GsonHttpMessageConverter 的默认转换器创建 RequestMappingHandlerAdapter.

It was causing creation of RequestMappingHandlerAdapter 2nd time with default converters without GsonHttpMessageConverter.

这篇关于强制 Spring-Boot 使用 Gson 而不是 Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆