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

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

李锋镝的博客

LiteLLM 本地代理搭建 Claude-HUD 使用文档 Kratos+ —— Kratos 主题二次开发记录 译文:如何将单体应用拆解为微服务 codebase-memory-mcp 极简完整使用指南 Claude Haiku 4.5、Claude Sonnet 4.6、Claude Opus 4.7 区别以及各自的新特性 SchedulingConfigurer详解 踩坑60+次后,我终于搞懂 Claude Skill 怎么写才会真的触发 Everything Claude Code 详细使用文档 这个域名注册整整十年了,十年时间,真快啊 Claude Code全维度实战指南:从入门到精通,解锁AI编程新范式 Apollo配置中心中的protalDB的作用是什么 org.apache.ibatis.plugin.Interceptor类详细介绍及使用 岁末 Excel2016右键新建工作表,打开时提示“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决办法 wordpress增加说说功能 玩博客的人是不是越来越少了? Java 为什么有这么多 “O”? 别再背线程池的七大参数了,现在面试官都这么问 2024年11月1号 农历十月初一 我的第一个WordPress插件:Dylan Custom Plugin上线了 推荐一款比较养眼的Xshell配色方案 hnswlib installation failed 开工啦~ 阳了... MybatisCodeHelperPro激活 @Async注解的坑 新买的笔记本发货啦…… 这个中秋节感觉过的好累啊 IDEA下载源码报:Cannot connect to the Maven process. Try again later. RocketMQ的push消费方式实现详解 看病难~取药难~~ 笑死、腹肌……根本不可能有腹肌的~~ 居家办公了~ C# 11 的这个新特性,我愿称之最强! IntelliJ IDEA 2020.3.x永久白嫖(Windows/Mac) IDEA无限试用方法【2020.3最新亲测有效】 SpringBoot使用注解的方式构建Elasticsearch查询语句,实现多条件的复杂查询 UUID太长怎么办?快来试试NanoId 忽然发现,在校大学生可以免费领一年有道云笔记会员~ 醒醒~补个税了 使用itext和freemarker来根据Html模板生成PDF文件,加水印、印章 来来来,用python画一个冰墩墩儿 SpringBoot整合GraphQL入门教程 办理居住证困难重重啊! 妈呀,昨天晚上睡觉做了一晚上的梦,可累死我了 感觉Typecho很简洁啊…… WordPress的自动更新好烦啊 BeanCopier工具类(性能优化工具类) 内存屏障浅析 居住证可算是申请通过了…… 试了下壁挂炉供暖 关于重阳节 居住证签注... Spring Boot 2.x使用PostgreSQL数据库 十一节后开工头一天,修了个耳机…… 玉楼春·尊前拟把归期说 nginx反向代理配置去除前缀 1931→2021! Navicat Premium数据库账号密码解密 百度的索引量数据现在有点儿太夸张了吧? 哇塞~这个小姐姐实在太惊艳了…… 关于8月29号下午博客故障的一些记录 如何形象的描述反应式编程中的背压(Backpressure)机制? 基于Java8的Either类 海琴烟~~~ JMX监控权限认证配置 IntelliJ IDEA 2019.3.3 永久激活 破解[Windows] SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码) SpringBoot整合Elasticsearch游标查询(scroll) GitLab创建新项目,初次提交命令和流程 吐槽下Google浏览器~ 从零搭建Spring Cloud Gateway网关(一) 从零搭建Spring Cloud Gateway网关(二)—— 打印请求响应日志 博客有logo啦 你的答案 数据库事务的一点简单总结 Spring Boot发展史(Spring Boot介绍) 【漫画】戏说外行对程序员的误会有多深 【收藏】从面试官角度观察到的程序员技能瓶颈,同时给出突破瓶颈的建议 Git中.gitignore文件不起作用的解决办法 背影 - 朱自清 jmap命令(jdk1.8) 彻底搞懂mysql日志系统binlog,redolog,undolog 出院了~~~ linux中ftp查看不到文件列表的问题 PHP版本怎么更新啊…… 成语接龙 StarUML4.0破解文件 Java SPI详解 WordPress评论框增加自定义表情 因在公司上不正经网站,我没过试用期… 优化了MYSQL大量写入问题,老板奖励了1000块给我 不慌不忙的坚强(林徽因39段最美文字!) ConcurrentHashMap常用方法源码解析(jdk1.8) 我是如何失去团队掌控的? HBASE填坑日志 关闭apache httpclient4.5 DEBUG日志 使用OpenShift搭建k8s集群 SonarQube Scanner的配置与使用简介
配置Jackson使用字段而不是getter/setter来序列化和反序列化
李锋镝 · 2026-03-19 · via 李锋镝的博客

要让Jackson直接使用类的字段(Field) 进行序列化和反序列化,而非默认的getter/setter方法,核心是修改Jackson的属性访问策略:关闭对getter/isGetter/setter的自动检测,开启对所有字段(无论访问修饰符)的检测。

下面分通用Java项目Spring Boot项目局部类注解三种场景,提供最实用的配置方案。


核心原理

Jackson默认通过以下规则映射属性:

  • 序列化:找getXxx()/isXxx()方法
  • 反序列化:找setXxx()方法

我们需要修改ObjectMapper的可见性配置:

  1. 禁用:GETTER/IS_GETTER/SETTER的检测
  2. 启用:FIELD(字段)的检测,并设置可见性为ANY(支持private/protected/public所有字段)

方式1:通用Java项目(全局配置)

直接手动配置ObjectMapper,全局生效,无需依赖框架。

步骤1:配置ObjectMapper

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonFieldConfig {
    public static ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();

        // 1. 禁用getter方法检测(序列化默认用getter)
        objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        // 2. 禁用布尔类型isGetter检测(如isAdult())
        objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        // 3. 禁用setter方法检测(反序列化默认用setter)
        objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        // 4. 启用字段检测,所有访问修饰符的字段都可见
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        return objectMapper;
    }
}

步骤2:测试实体类(无getter/setter)

// 无任何getter/setter,仅私有字段
public class User {
    // 私有字段,Jackson直接访问
    private String username;
    private int age;
    private boolean isStudent;

    // Jackson必须有无参构造器(默认省略,显式写更稳妥)
    public User() {}

    // 测试用构造器
    public User(String username, int age, boolean isStudent) {
        this.username = username;
        this.age = age;
        this.isStudent = isStudent;
    }
}

步骤3:序列化/反序列化测试

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = JacksonFieldConfig.getObjectMapper();

        // 1. 序列化(字段 → JSON)
        User user = new User("张三", 20, true);
        String json = mapper.writeValueAsString(user);
        System.out.println(json); 
        // 输出:{"username":"张三","age":20,"isStudent":true}

        // 2. 反序列化(JSON → 字段)
        User user2 = mapper.readValue(json, User.class);
        System.out.println(user2.username); // 张三
        System.out.println(user2.isStudent); // true
    }
}

方式2:Spring Boot项目(全局自动配置)

Spring Boot默认自动装配ObjectMapper,只需自定义Bean覆盖默认配置,全局生效。

步骤1:创建Jackson配置类

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {
    /**
     * 自定义全局ObjectMapper,使用字段而非getter/setter
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();

        // 同通用配置,禁用方法检测,启用字段检测
        objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        return objectMapper;
    }
}

配置后,Spring所有场景(@ResponseBody、Redis序列化、接口参数)都会直接用字段处理JSON。


方式3:局部类注解(仅单个类生效)

如果只需要某个类用字段,其他类仍用getter/setter,直接在类上添加@JsonAutoDetect注解即可,无需全局配置。

import com.fasterxml.jackson.annotation.JsonAutoDetect;

// 仅当前类使用字段序列化/反序列化
@JsonAutoDetect(
    fieldVisibility = JsonAutoDetect.Visibility.ANY,       // 字段可见
    getterVisibility = JsonAutoDetect.Visibility.NONE,     // 禁用getter
    isGetterVisibility = JsonAutoDetect.Visibility.NONE,  // 禁用isGetter
    setterVisibility = JsonAutoDetect.Visibility.NONE      // 禁用setter
)
public class Product {
    private Long id;
    private String productName;
    private double price;

    // 无getter/setter
    public Product() {}
}

方式4:字段级注解(仅单个字段生效)

如果只需要个别字段用字段访问,其余仍用getter/setter,给目标字段加@JsonProperty即可。

import com.fasterxml.jackson.annotation.JsonProperty;

public class Order {
    // 强制用字段序列化/反序列化
    @JsonProperty
    private String orderNo;

    // 仍用getter/setter
    private Integer amount;

    // getter/setter
    public Integer getAmount() {return amount;}
    public void setAmount(Integer amount) {this.amount = amount;}
}

总结

  1. 全局推荐:通用项目用ObjectMapper手动配置,Spring Boot用@Bean自定义ObjectMapper
  2. 局部场景:单个类用@JsonAutoDetect,单个字段用@JsonProperty
  3. 核心配置FIELD设为ANYGETTER/IS_GETTER/SETTER设为NONE

配置后,Jackson会完全跳过getter/setter,直接读写类的字段(包括私有字段)。

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/hou-duan/4694