惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
B
Blog
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
B
Blog RSS Feed
WordPress大学
WordPress大学
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
O
OpenAI News
月光博客
月光博客
H
Hacker News: Front Page
S
Security Affairs
W
WeLiveSecurity
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
A
About on SuperTechFans

czp's blog

Ubuntu 下使用 zsh · HonKit 使用 VNC 来远程管理服务器 · HonKit Linux 下无限试用 JetBrains · HonKit 使用 KMS 激活 Windows · HonKit 为 KVM 中的 Windows 虚拟机启用 VirtIO · HonKit 在 Spring Boot 中处理 MissingKotlinParameterException · HonKit 用 Spring Native 拯救微服务 · HonKit 正确调试 PHP · HonKit Ubuntu 增加最大文件打开数量限制 · HonKit 将 Minecraft 服务端编译到 native · HonKit 用凝聚力购买 P 社游戏 DLC · HonKit 模拟超星网课 Android 客户端 · HonKit 模拟 Bilibili Android 客户端 · HonKit 模拟哔咔 Android 客户端 · HonKit 异步是什么 · HonKit GCP Pub/Sub push 至 IAP 保护的 Endpoint · HonKit 部署 Spring Native 到 GCP App Engine · HonKit 多个 Gradle SubProject 同时编译导致 CI/CD 内存耗尽 · HonKit Google Cloud Build 运行 DIND · HonKit 在 Google Storage 中部署 SPA · HonKit Terraform 不自动更新 CloudRun service · HonKit Telegram Bot 收不到普通群聊消息的问题 · HonKit 加密货币交易 · HonKit 挖掘以太坊 · HonKit 挖掘门罗币 · HonKit 小明学英语 · HonKit 卖程序的小女孩 · HonKit 一个测试工程师走进一家酒吧 · HonKit 我给你们讲个 TCP 笑话吧! · HonKit 我给你们讲个 UDP 笑话吧! · HonKit 格局打开 · HonKit c 语言常见未定义行为 · HonKit 在 Kotlin 使用 SpaceEngineers Remote API · HonKit Kotlin 协程 · HonKit Spring Boot Jpa 使用 Google Cloud SQL · HonKit Spring Boot 中配置单页应用 · HonKit Spring Boot 无法加载 ClasspathResource 问题 · HonKit 正确打包 Spring Boot 到 war · HonKit czp's blog · HonKit 鸣谢列表 · HonKit 友链 · HonKit
在 Spring Boot 中正确注册 Jackson Module · HonKit
2026-04-11 · via czp's blog

本文撰写时的 Spring 版本: spring-boot 2.0.5.RELEASE

当我们在进行 Spring Boot 开发时, REST 接口的默认返回类型是 json, 使用的序列化库为 jackson.

Spring Boot 内部使用的 ObjectMapper 是在 MappingJackson2HttpMessageConverter 里被配置好的. 于是问题就来了, 我们想要使用更多的 Jackson Module 怎么办.

默认被配置的 Jackson ModuleJdk8Module, JavaTimeModule, JodaModule, KotlinModule. 这些 Module 是在哪里被注册的呢, 通过搜索大法, 我们发现是在这里 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.configure(ObjectMapper objectMapper)

Assert.notNull(objectMapper, "ObjectMapper must not be null");

if (this.findModulesViaServiceLoader) {
    objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader));
}
else if (this.findWellKnownModules) {
    registerWellKnownModulesIfAvailable(objectMapper);
}

...

通过 IDEAfind usage 功能我们可以发现, findModulesViaServiceLoader 这个字段永远是 false, 根本没有方法会调用他的 setter. 更加不要提 JacksonProperties 这个设置类了, 所以这段程序永远都会走到 else if 分支.

而所谓的 WellKnownModules 就是内定的那几个 Module.

我们假设 jackson-datatype-hibernate5 是我们想要注册的 Module.

我们查看他的 jar 包会发现 services 目录里面有一个文件叫做 com.fasterxml.jackson.databind.Module. ObjectMapper.findModules(this.moduleClassLoader) 这个方法就是靠这个文件来判断 classpath 中哪些包是他的 Module.

但是因为 findModulesViaServiceLoader 永远是 false, 所以并非是只要加入依赖就可以实现 Module 的自动注册了.

KotlinModule 只要被加入依赖就可以自动注册只是因为他是内定的几个模块之一.

所以简单的加入依赖是没有作用的, 我们必须要用代码来让 Spring Boot 知道我们想要使用这个 Module.

于是我们在谷歌上搜索 spring boot jackson hibernate, 结果搜到的文章都是教人直接替换 MappingJackson2HttpMessageConverter, 可真是一个小机灵鬼.

而一旦替换这个类, 就要把它原本的代码再写一遍, Spring 版本升级之后可能还会挂掉, 所以正确做法一定不是这样.

根据 Spring Boot 标准命名法, 自动配置类的类名后缀为 AutoConfiguration, 于是我们找到了自动配置 Jackson 的地方 org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration

然后我们看到在这里 JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration.StandardJack2ObjectMapperBuilderCustomizer.configreModules(Jackson2ObjectMapperBuilder builder)

private void configureModules(Jackson2ObjectMapperBuilder builder) {
    Collection<Module> moduleBeans = getBeans(this.applicationContext,
            Module.class);
    builder.modulesToInstall(moduleBeans.toArray(new Module[0]));
}

applicationContext 中的所有 Module 实现类被自动注册了!

所以实际上我们只需要有一个 Bean 就可以实现 Module 的自动注册了.

@Configuration
open class JacksonHibernate5Configuration {
    @Bean
    open fun dataTypeHibernateModule() = Hibernate5Module()
}

就这么简单么? 对, 就是这么简单(这么简单还绕了那么大一圈, 真的菜)!