@JSONProperty注解详解:Spring Boot中处理JSON序列化与反序列化的利器

@JSONProperty注解详解

什么是@JSONProperty注解

@JSONProperty是Jackson库提供的一个注解,用于控制Java对象属性在JSON序列化和反序列化过程中的行为。它允许我们自定义JSON字段名、访问顺序、是否包含某个字段等。

基本用法

1. 修改JSON字段名

java 复制代码
public class User {
    @JSONProperty("user_name")
    private String name;
    
    @JSONProperty("user_age")
    private Integer age;
    
    // getter和setter方法
}

这样,当将User对象转换为JSON时,属性名将变为user_name和user_age,而不是默认的驼峰命名。

2. 设置属性顺序

java 复制代码
public class Order {
    @JSONProperty(ordinal = 1)
    private String orderId;
    
    @JSONProperty(ordinal = 3)
    private BigDecimal amount;
    
    @JSONProperty(ordinal = 2)
    private Date createTime;
    
    // getter和setter方法
}

通过ordinal属性可以控制JSON输出中字段的顺序。

3. 条件性包含属性

java 复制代码
public class Product {
    private String id;
    
    @JSONProperty(includeIfNull = false)
    private String description;
    
    @JSONProperty(serialze = false)
    private String internalCode;
    
    // getter和setter方法
}
  • includeIfNull = false:当属性值为null时不包含在JSON中
  • serialze = false:禁止序列化该属性

在Spring Boot开发MCP中的实际应用

在Spring Boot项目中,特别是开发微服务(Microservices)或API网关时,经常需要处理不同系统间的数据格式转换问题。@JSONProperty注解可以帮助我们解决以下常见问题:

1. 统一API响应格式

java 复制代码
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @GetMapping("/{id}")
    public ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
        User user = userService.findById(id);
        return ResponseEntity.ok(user.toResponse());
    }
}

// DTO类
public class UserResponse {
    @JSONProperty("userId")
    private Long id;
    
    @JSONProperty("fullName")
    private String name;
    
    @JSONProperty("emailAddress")
    private String email;
    
    @JSONProperty("registrationDate")
    private LocalDateTime registerDate;
    
    // 构造函数、getter和setter方法
    
    public static UserResponse from(User user) {
        UserResponse response = new UserResponse();
        response.id = user.getId();
        response.name = user.getName();
        response.email = user.getEmail();
        response.registerDate = user.getCreatedAt();
        return response;
    }
}

2. 处理数据库字段名与API字段名的不一致

java 复制代码
@Entity
@Table(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "user_name")
    private String name;
    
    @Column(name = "user_email")
    private String email;
    
    @Column(name = "create_time")
    private LocalDateTime createdAt;
    
    // getter和setter方法
}

// 使用@JSONProperty保持一致性
public class UserDTO {
    @JSONProperty("userId")
    private Long id;
    
    @JSONProperty("name")
    private String name;
    
    @JSONProperty("email")
    private String email;
    
    @JSONProperty("createdAt")
    private LocalDateTime createdAt;
    
    // 构造函数、getter和setter方法
}

3. 敏感信息过滤

java 复制代码
public class UserProfile {
    private String id;
    
    private String username;
    
    @JSONProperty(serialze = false)
    private String password;
    
    @JSONProperty(serialze = false)
    private String securityQuestion;
    
    @JSONProperty(serialze = false)
    private String securityAnswer;
    
    // getter和setter方法
}

高级用法

1. 自定义命名策略

java 复制代码
public class CustomNamingStrategy implements NamingStrategy {
    @Override
    public String translate(String input) {
        // 实现自定义命名规则,如全部大写、添加前缀等
        return "PREFIX_" + input.toUpperCase();
    }
    
    @Override
    public String getClassName(String input, boolean lowerCase) {
        return translate(input);
    }
    
    @Override
    public String[] split(String input) {
        // 实现字符串分割逻辑
        return input.split("_");
    }
}

// 配置Jackson使用自定义命名策略
@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new CustomNamingStrategy());
        return mapper;
    }
}

2. 条件性序列化

java 复制代码
public class ConditionalUser {
    private String id;
    
    private String name;
    
    @JSONProperty(serialzeUsing = SerializationFeature.class)
    private String role;
    
    @JSONProperty(deserialzeUsing = DeserializationFeature.class)
    private String permissions;
    
    // getter和setter方法
}

3. 多态序列化

java 复制代码
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Car.class, name = "car"),
    @JsonSubTypes.Type(value = Bike.class, name = "bike")
})
public abstract class Vehicle {
    private String brand;
    
    @JSONProperty("vehicleBrand")
    public String getBrand() {
        return brand;
    }
}

public class Car extends Vehicle {
    private int numberOfDoors;
    
    @JSONProperty("doors")
    public int getNumberOfDoors() {
        return numberOfDoors;
    }
}

public class Bike extends Vehicle {
    private boolean hasBasket;
    
    @JSONProperty("hasStorage")
    public boolean isHasBasket() {
        return hasBasket;
    }
}

总结

@JSONProperty注解为Spring Boot应用中的JSON处理提供了极大的灵活性。通过使用这个注解,我们可以:

  1. 统一API接口的数据格式,提高一致性
  2. 隐藏内部实现细节,只暴露必要的字段
  3. 处理数据库字段名与API字段名的差异
  4. 控制JSON输出的顺序
  5. 实现条件性序列化和反序列化

在实际开发中,合理使用@JSONProperty注解能够显著提升代码的可维护性和API的专业度。特别是在微服务架构中,不同服务间的数据格式标准化变得尤为重要,@JSONProperty注解成为了不可或缺的利器。

相关推荐
Cosolar7 小时前
不收费、不加水印、数据归你:我做了个开源的在线简历工具
后端·面试·前端框架
小蒜学长8 小时前
基于Spring Boot+Vue3的在线医疗咨询系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·在线问诊·在线医疗咨询系统
陈随易8 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
每天一道题9 小时前
CAP、Raft 与多数派:分布式系统如何达成一致?
分布式·后端·微服务
前端的日常9 小时前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
IT_陈寒10 小时前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端
ServBay10 小时前
本地模型已经跑起来了,Copilot Fusion 为什么还要联网?
后端·ai编程·github copilot
武子康11 小时前
H3-World 的动作没按时发生,先检查时间线变成了什么
人工智能·后端·agent
苍何11 小时前
腾讯 Buddy One 来了,办公全家桶一键打通WorkBuddy(附教程)
后端
GIS数据转换器11 小时前
基于CIM的城市智慧供热监管服务平台
java·大数据·开发语言·人工智能·后端·物联网·struts