互相交流入口地址
整體目錄:
【一】springboot整合swagger
【二】springboot整合自定義swagger
【三】springboot整合token
【四】springboot整合mybatis-plus
【五】springboot整合mybatis-plus
【六】springboot整合redis
【七】springboot整合AOP實現日志操作
【八】springboot整合定時任務
【九】springboot整合redis實現啟動服務時熱點數據保存在全局和緩存
【十】springboot整合quartz實現定時任務優化
【十一】springboot整合異步調用并獲取返回值
【十二】springboot整合WebService
【十三】springboot整合WebService關于傳參數
【十四】springboot整合WebSocket
【十五】springboot整合WebSocket實現聊天室
【十六】RabbitMQ基礎篇(下載安裝并基礎使用,內含各種坑問題)
【十七】RabbitMQ基礎篇(延遲隊列和死信隊列實戰)
【十八】springboot實現自定義全局異常處理
【十九】初學Kafka并實戰整合SpringCloudStream進行使用
【二十】springboot整合ElasticSearch實戰(萬字篇)
【二十一】springboot整合過濾器實戰
【二十二】springboot整合攔截器實戰并對比過濾器
【二十三】springboot整合activiti7(1)實戰演示篇
【二十四】springboot整合spring事務詳解以及實戰
【二十五】springboot使用EasyExcel和線程池實現多線程導入Excel數據
【二十六】springboot整合jedis和redisson布隆過濾器處理緩存穿透
【二十七】springboot實現多線程事務處理
【二十八】springboot之threadLocal參數解析器實現session一樣保存當前登錄功能?
【二十九】springboot整合logback實現日志管理
【三十】springboot項目上高并發解決示例
【三十一】springboot+easyExcel實現多文件導出壓縮包
目錄
一、返回值脫敏
二、返回值日期格式化?
? ? ? ? 很久沒有寫小作文了,趕著學子們參加考試的時間,繼續記錄點小東西,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;}@Overridepublic 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*/@Overridepublic 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;}@Overridepublic void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString(DateUtil.format(DateUtil.parse(value,sourceFormat), targetFormat));}@Overridepublic 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的時間格式都能夠成功處理。
? ? ? ? 歡迎大佬們評論區討論。?