赞
踩
互相交流入口地址
整体目录:
【九】springboot整合redis实现启动服务时热点数据保存在全局和缓存
【十三】springboot整合WebService关于传参数
【十五】springboot整合WebSocket实现聊天室
【十六】RabbitMQ基础篇(下载安装并基础使用,内含各种坑问题)
【十九】初学Kafka并实战整合SpringCloudStream进行使用
【二十】springboot整合ElasticSearch实战(万字篇)
【二十三】springboot整合activiti7(1)实战演示篇
【二十四】springboot整合spring事务详解以及实战
【二十五】springboot使用EasyExcel和线程池实现多线程导入Excel数据
【二十六】springboot整合jedis和redisson布隆过滤器处理缓存穿透
【二十八】springboot之threadLocal参数解析器实现session一样保存当前登录功能
目录
很久没有写小作文了,赶着学子们参加考试的时间,继续记录点小东西,1、返回对象的字符串数据脱敏 ;2、返回对象针对字符串格式的时间的格式化。
1、准备返回值对象
2、准备接口
3、准备脱敏注解
4、准备序列化处理类
- 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、准备序列化处理类
- 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、测试效果
可看到字符串格式的时间,包括含有T的时间格式都能够成功处理。
欢迎大佬们评论区讨论。
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=ebsdh70fmgsh
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。