当前位置:   article > 正文

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

spring boot 返回日期序列化

e354e0e7434147d2a6654a29341a028d.gif

互相交流入口地址

整体目录:

【一】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、准备返回值对象

e54fb351994c4cd49a6ac076681e8653.png

       2、准备接口

2acaefd4ce714082acf5da976ba8808d.png

        3、准备脱敏注解

a04aa8294ea14091ae4a87f5bfde191f.png

        4、准备序列化处理类

  1. public class SensitiveInfoSerialize extends JsonSerializer<String> implements ContextualSerializer {
  2. private DesensitizationType type;
  3. public SensitiveInfoSerialize() {
  4. }
  5. public SensitiveInfoSerialize(final DesensitizationType type) {
  6. this.type = type;
  7. }
  8. @Override
  9. public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  10. switch (this.type) {
  11. case ID_CARD:
  12. value = DesensitizedUtil.idCardNum(value, 4, 2);
  13. break;
  14. case MOBILE_PHONE: {
  15. value = DesensitizedUtil.mobilePhone(value);
  16. break;
  17. }
  18. default:
  19. break;
  20. }
  21. gen.writeString(value);
  22. }
  23. /**
  24. * 序列化时获取字段注解属性
  25. * @param serializerProvider
  26. * @param property
  27. * @return
  28. * @throws JsonMappingException
  29. */
  30. @Override
  31. public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
  32. if (property != null) {
  33. // 此demo只处理String类型字段
  34. if (Objects.equals(property.getType().getRawClass(), String.class)) {
  35. SensitiveInfo sensitiveInfo = property.getAnnotation(SensitiveInfo.class);
  36. if (sensitiveInfo == null) {
  37. sensitiveInfo = property.getContextAnnotation(SensitiveInfo.class);
  38. }
  39. if (sensitiveInfo != null) {
  40. return new SensitiveInfoSerialize(sensitiveInfo.value());
  41. }
  42. }
  43. return serializerProvider.findValueSerializer(property.getType(), property);
  44. }
  45. return serializerProvider.findNullValueSerializer(null);
  46. }
  47. }

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

        5、演示原本效果

609069c072834bddb6b75c064f56bd0a.png

        6、增加注解后效果

d9f617b9b6c3484c9bc9f501ce93076a.png

a5452f725aea4e7ebcb51b6145e643ee.png

二、返回值日期格式化 

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

        1、返回值增加时间字段

b12adfed3e244f96a48e81b8c8a69bc1.png

7cfca5ce81cf4497a75dc1f6bd50b6e8.png

        2、原有效果

d50ba95671a641729c81bb37c6f5936f.png

862d18ee38c54f12a70b4d590fb10d6a.png

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

2131f2446b9a4359b35d5bf382d357be.png

e374b93d9b4440a1b2f5cfe03b80f0c0.png

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

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

eba5366affae412aaf82596da47357bc.png

        5、准备序列化处理类

  1. public class StringToDateSerialize extends JsonSerializer<String> implements ContextualSerializer {
  2. private String sourceFormat;
  3. private String targetFormat;
  4. public StringToDateSerialize() {
  5. }
  6. public StringToDateSerialize(final String sourceFormat, final String targetFormat) {
  7. this.sourceFormat = sourceFormat;
  8. this.targetFormat = targetFormat;
  9. }
  10. @Override
  11. public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  12. gen.writeString(DateUtil.format(DateUtil.parse(value,sourceFormat), targetFormat));
  13. }
  14. @Override
  15. public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty property) throws JsonMappingException {
  16. if (property != null) {
  17. if (Objects.equals(property.getType().getRawClass(), String.class)) {
  18. StringToDate stringToDate = property.getAnnotation(StringToDate.class);
  19. if (stringToDate == null) {
  20. stringToDate = property.getContextAnnotation(StringToDate.class);
  21. }
  22. if (stringToDate != null) {
  23. return new StringToDateSerialize(stringToDate.source(),stringToDate.target());
  24. }
  25. }
  26. return serializerProvider.findValueSerializer(property.getType(), property);
  27. }
  28. return serializerProvider.findNullValueSerializer(null);
  29. }
  30. }

        6、测试效果

ce40338239ff4bc7acb94d45de922752.png

f786ea99c85941ee8b83eb94dfcbb054.png

a9da90e88f0e4fa09bc0053c5b0eef90.png

        可看到字符串格式的时间,包括含有T的时间格式都能够成功处理。

        欢迎大佬们评论区讨论。 

 

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=ebsdh70fmgsh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/920923
推荐阅读
相关标签
  

闽ICP备14008679号