注解+aop⼀:定义⼀个注解
⼆:在序列化时候,判断字段上是否有注解,进⾏序列化的操作三:在对应的字段上添加上注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveWordSerializer.class)public @interface SensitiveWord {
SensitiveWordType sensitiveWordType() default SensitiveWordType.MOBILE ;}
public class SensitiveWordSerializer extends JsonSerializer @Override public JsonSerializer> createContextual(SerializerProvider prov,BeanProperty property) throws JsonMappingException { SensitiveWord sensitiveWord = property.getAnnotation(SensitiveWord.class); if (sensitiveWord != null) { sensitiveWordType = sensitiveWord.sensitiveWordType(); } return this ; } @Override public void serialize(String value,JsonGenerator gen,SerializerProvider serializers) throws IOException, JsonProcessingException { if (!StringUtils.hasText(value)) { gen.writeString(value); return; } if (sensitiveWordType != null) { value = value.replaceAll(sensitiveWordType.getRegex() , sensitiveWordType.getReplacement()); } gen.writeString(value); }} @SensitiveWord @ApiModelProperty(value = \"机构联系⽅式\ private String contact; 结果 注解+序列化的实现 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务