自定义 Jackson ObjectMapper 以读取自定义注释和蒙版带注释的字段
2022-09-03 14:25:11
我有一个要求,我已经创建了一个自定义注释@MaskSensitiveData。我注释敏感字段。喜欢
class MyBean {
String userName;
@MaskSensitiveData
String cardNumber;
String abc;
String xyz;
}
ObjectMapper mapper = new ObjectMapper();
String json = null;
AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
mapper.setAnnotationIntrospector(pair);
try {
json = mapper.writeValueAsString(obj);
/*
* if(json != null ) { json = getLoggableString(json); }
*/
} catch (Exception e) {
return "Unable to convert to Json object:" + obj.toString() + " Message: " + e.getMessage();
}
我正在使用Jackson ObjectMapper将objct转换为Json like。我需要自定义对象映射器以在返回json中屏蔽cardNumber字段。请建议更好的方法。