From beb6b074f7874e07c91c84f0172436cf18caa229 Mon Sep 17 00:00:00 2001 From: yuejiajun <1530620364@qq.com> Date: Tue, 30 Sep 2025 16:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/common/filter/CustomJsonFilter.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/example/demo/common/filter/CustomJsonFilter.java diff --git a/src/main/java/com/example/demo/common/filter/CustomJsonFilter.java b/src/main/java/com/example/demo/common/filter/CustomJsonFilter.java new file mode 100644 index 0000000..c22c1d2 --- /dev/null +++ b/src/main/java/com/example/demo/common/filter/CustomJsonFilter.java @@ -0,0 +1,74 @@ +package com.example.demo.common.filter; + +import lombok.extern.slf4j.Slf4j; + +import java.util.*; + +@Slf4j +public class CustomJsonFilter { + + @SuppressWarnings("all") + @Override + public boolean equals(Object obj) { + if (obj == null) { + return true; + } + boolean flag = !shouldInclude(obj); + log.trace("{} => {}", flag, obj); + return flag; + } + + /** + * 判断值是否应该被包含 + */ + private boolean shouldInclude(Object value) { + // 处理空字符串 + if (value instanceof CharSequence) { + return !((CharSequence) value).isEmpty(); + } + + // 处理空集合 + if (value instanceof Collection) { + return !((Collection) value).isEmpty(); + } + // 处理空Map + if (value instanceof Map) { + return !((Map) value).isEmpty(); + } + // 处理空数组 + if (value instanceof Object[]) { + return ((Object[]) value).length > 0; + } + + // 处理基本类型数组 + if (value instanceof int[]) { + return ((int[]) value).length > 0; + } + if (value instanceof long[]) { + return ((long[]) value).length > 0; + } + if (value instanceof double[]) { + return ((double[]) value).length > 0; + } + if (value instanceof boolean[]) { + return ((boolean[]) value).length > 0; + } + if (value instanceof char[]) { + return ((char[]) value).length > 0; + } + if (value instanceof byte[]) { + return ((byte[]) value).length > 0; + } + if (value instanceof float[]) { + return ((float[]) value).length > 0; + } + + // 处理空Optional + if (value instanceof Optional) { + return ((Optional) value).isPresent(); + } + + // 其他对象类型,只要不为null就包含 + return true; + } +}