前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【三十三】springboot+序列化实现返回值脱敏和返回值字符串时间格式化问题

【三十三】springboot+序列化实现返回值脱敏和返回值字符串时间格式化问题

作者头像
小z666
发布2024-06-21 17:32:49
960
发布2024-06-21 17:32:49
举报
文章被收录于专栏:javajava

一、返回值脱敏

1、准备返回值对象

2、准备接口

3、准备脱敏注解

4、准备序列化处理类

代码语言:javascript
复制
public class SensitiveInfoSerialize extends JsonSerializer<String> implements ContextualSerializer {

    private DesensitizationType type;

    public SensitiveInfoSerialize() {
    }

    public SensitiveInfoSerialize(final DesensitizationType type) {
        this.type = type;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        switch (this.type) {
            case ID_CARD:
                value = DesensitizedUtil.idCardNum(value, 4, 2);
                break;
            case MOBILE_PHONE: {
                value = DesensitizedUtil.mobilePhone(value);
                break;
            }
            default:
                break;
        }
        gen.writeString(value);
    }

    /**
     * 序列化时获取字段注解属性
     * @param serializerProvider
     * @param property
     * @return
     * @throws JsonMappingException
     */
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
        if (property != null) {
            // 此demo只处理String类型字段
            if (Objects.equals(property.getType().getRawClass(), String.class)) {
                SensitiveInfo sensitiveInfo = property.getAnnotation(SensitiveInfo.class);
                if (sensitiveInfo == null) {
                    sensitiveInfo = property.getContextAnnotation(SensitiveInfo.class);
                }
                if (sensitiveInfo != null) {
                    return new SensitiveInfoSerialize(sensitiveInfo.value());
                }
            }
            return serializerProvider.findValueSerializer(property.getType(), property);
        }
        return serializerProvider.findNullValueSerializer(null);
    }

}

实现ContextualSerializer接口后重写的JsonSerializer方法就是为了找到需要处理的属性,而集成JsonSerializer后重写的serialize方法就是为了处理需要处理的属性。DesensitizedUtil是糊涂的工具。就这样就可以了。

5、演示原本效果

6、增加注解后效果

二、返回值日期格式化

在开发时返回值里的时间一定不只是Date、LocalDateTime、LocalDate,有时候也可能是字符串格式。此时常用的@JsonFormat注解就失去用武之地了,使用上面的方式也可以处理这种情况,下面进行展示。

1、返回值增加时间字段

2、原有效果

3、使用常用的@JsonFormat注解进行处理

处理字符串的时间以外,其他的时间都能正常处理,下面通过序列化的方式进行处理该字段。

4、增加字符串日期格式处理注解

5、准备序列化处理类

代码语言:javascript
复制
public class StringToDateSerialize extends JsonSerializer<String> implements ContextualSerializer {

    private String sourceFormat;

    private String targetFormat;

    public StringToDateSerialize() {
    }

    public StringToDateSerialize(final String sourceFormat, final String targetFormat) {
        this.sourceFormat = sourceFormat;
        this.targetFormat = targetFormat;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(DateUtil.format(DateUtil.parse(value,sourceFormat), targetFormat));
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
        if (property != null) {
            if (Objects.equals(property.getType().getRawClass(), String.class)) {
                StringToDate stringToDate = property.getAnnotation(StringToDate.class);
                if (stringToDate == null) {
                    stringToDate = property.getContextAnnotation(StringToDate.class);
                }
                if (stringToDate != null) {
                    return new StringToDateSerialize(stringToDate.source(),stringToDate.target());
                }
            }
            return serializerProvider.findValueSerializer(property.getType(), property);
        }
        return serializerProvider.findNullValueSerializer(null);
    }

}

6、测试效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二、返回值日期格式化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档