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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

文章列表

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类详细介绍及使用
配置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