暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SimplePropertyPreFilter和@JsonIgnore

soul0202 2025-07-16
144

SimplePropertyPreFilter类
这段代码定义了一个名为 PropertyPreExcludeFilter 的类,它继承自 SimplePropertyPreFilter。这个类的主要作用是排除 JSON 序列化过程中的敏感属性。让我们详细分析一下这段代码的用途和使用场景。

代码分析
类定义
/**
* 排除JSON敏感属性
*
* @author ruoyi
*/
public class PropertyPreExcludeFilter extends SimplePropertyPreFilter
{
public PropertyPreExcludeFilter()
{
}

public PropertyPreExcludeFilter addExcludes(String... filters)
{
for (int i = 0; i < filters.length; i++)
{
this.getExcludes().add(filters[i]);
}
return this;
}
}

关键点
继承 SimplePropertyPreFilter:

PropertyPreExcludeFilter 类继承自 SimplePropertyPreFilter,这意味着它要么扩展了 SimplePropertyPreFilter 的功能,要么在其基础上进行了特殊化。
SimplePropertyPreFilter 通常是一个用于过滤 JSON 序列化过程中属性的类。
构造函数:

PropertyPreExcludeFilter():这个构造函数不做任何事情,它只是简单地调用了父类的构造函数。
addExcludes 方法:

public PropertyPreExcludeFilter addExcludes(String... filters):这个方法允许你传入一个或多个字符串,这些字符串表示需要从 JSON 序列化过程中排除的属性名。
方法内部通过循环遍历传入的 filters 数组,并将每个属性名添加到 getExcludes() 返回的集合中。
这个方法返回 PropertyPreExcludeFilter 本身,这是为了支持链式调用(Method Chaining)。
使用场景
PropertyPreExcludeFilter 的使用场景主要是在需要排除某些敏感或不需要序列化的属性时。具体场景包括但不限于:

敏感信息过滤:

当你需要将 Java 对象序列化为 JSON 格式时,可能有一些敏感属性(如密码、密钥等)是不应该被序列化的。使用 PropertyPreExcludeFilter 可以轻松地将这些敏感属性从 JSON 输出中排除。
性能优化:

在某些情况下,你可能只想序列化对象的部分属性,而不是全部属性。排除不需要的属性可以减少 JSON 输出的体积,从而提高序列化性能。
数据安全:

通过排除敏感属性,可以防止这些数据在传输或存储过程中被意外暴露,从而提高数据的安全性。
示例
假设你有一个包含用户信息的类 User,其中包含用户的敏感信息(如密码):

public class User {
private String username;
private String password;
private String email;

// Getters and setters
}
你可以使用 PropertyPreExcludeFilter 来排除 password 属性:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;

public class Main {
public static void main(String[] args) {
User user = new User();
user.setUsername("john_doe");
user.setPassword("secret");
user.setEmail("john_doe@example.com");

PropertyPreExcludeFilter filter = new PropertyPreExcludeFilter();
filter.addExcludes("password");

String jsonString = JSON.toJSONString(user, (SerializeFilter) filter);
System.out.println(jsonString);
}
}

输出结果
运行上述代码后,输出结果将会是:
{"email":"john_doe@example.com","username":"john_doe"}

总结
PropertyPreExcludeFilter 类通过继承 SimplePropertyPreFilter 并提供 addExcludes 方法,使得你可以灵活地排除 JSON 序列化过程中不需要的属性。这种过滤机制在很多需要控制 JSON 输出的场景中非常有用,尤其是在涉及敏感信息或性能优化的场合。

@JsonIgnore
@JsonIgnore 是 Jackson 库中的一个注解,用于在 JSON 序列化和反序列化过程中忽略某个字段。它通常应用于字段级别。而 PropertyPreExcludeFilter(根据你的代码示例,可能是基于 FastJSON 的一个类)则是 FastJSON 库中的一个过滤器,用于在序列化过程中排除指定的字段。

虽然它们的目的都是排除某些字段在 JSON 序列化过程中被包含,但它们的实现方式和使用场景略有不同。

区别
注解 vs 过滤器:

@JsonIgnore 是一个注解,可以直接标记在 Java 类的字段上。它的作用范围是特定的字段。
PropertyPreExcludeFilter 是一个过滤器,需要在序列化过程中显式地应用。它的作用范围是通过编程方式指定的。
作用域:

@JsonIgnore 是静态的,一旦应用在字段上,该字段在所有序列化过程中都会被忽略。
PropertyPreExcludeFilter 是动态的,你可以在运行时决定哪些字段需要被排除。
灵活性:

@JsonIgnore 适用于静态场景,当你明确知道哪些字段需要被忽略时,使用 @JsonIgnore 更为方便。
PropertyPreExcludeFilter 适用于动态场景,当你需要在不同的上下文中排除不同的字段时,使用过滤器更为灵活。
示例对比
使用 @JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class User {
private String username;
@JsonIgnore
private String password;
private String email;

// Getters and setters
}

public class Main {
public static void main(String[] args) throws Exception {
User user = new User();
user.setUsername("john_doe");
user.setPassword("secret");
user.setEmail("john_doe@example.com");

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(user);
System.out.println(jsonString);
}
}

使用 PropertyPreExcludeFilter
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;

public class User {
private String username;
private String password;
private String email;

// Getters and setters
}

public class Main {
public static void main(String[] args) {
User user = new User();
user.setUsername("john_doe");
user.setPassword("secret");
user.setEmail("john_doe@example.com");

PropertyPreExcludeFilter filter = new PropertyPreExcludeFilter();
filter.addExcludes("password");

String jsonString = JSON.toJSONString(user, (SerializeFilter) filter);
System.out.println(jsonString);
}
}
输出结果
两种方式的输出结果相同:
{"email":"john_doe@example.com","username":"john_doe"}

总结
@JsonIgnore:适用于静态场景,标记在字段上,全局有效。
PropertyPreExcludeFilter:适用于动态场景,通过编程方式在序列化过程中排除字段。
虽然它们的效果相似,但选择哪种方式取决于你的具体需求:如果需要静态排除某些字段,使用 @JsonIgnore 更为方便;如果需要在不同的上下文中动态排除字段,使用 PropertyPreExcludeFilter 更为灵活。

文章转载自soul0202,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论