修改jackson的JsonIgnoreProperties注解元素值
JsonIgnoreProperties jsonIgnoreProperties = entity.getClass().getAnnotation(JsonIgnoreProperties.class)
if(jsonIgnoreProperties){
InvocationHandler invocationHandler = Proxy.getInvocationHandler(jsonIgnoreProperties)
Field field = invocationHandler.class.getDeclaredField("memberValues")
**因为memberValues元素是private final修饰的,需要打开权限**
field.setAccessible(true);
Map fields = field.get(invocationHandler) as Map
String[] values = fields.get("value")
if(values){
String[] newValues = []
values.each { val ->
try {
if(entity.getClass().getDeclaredField(val).type == entity){
newValues << val
}
}catch (NoSuchFieldException e)
{ log.error("${entity}没有此属性${val}") }
}
fields.put("value",newValues)
}
}
**改完可以new一个实例看看效果**
JsonIgnoreProperties testJsonIgnoreProperties = new TestEntity().getClass().getAnnotation(JsonIgnoreProperties.class)
if(testJsonIgnoreProperties) {
InvocationHandler handler = Proxy.getInvocationHandler(testJsonIgnoreProperties)
Field testField = handler.class.getDeclaredField("memberValues")
testField.setAccessible(true);
Map fields = testField.get(handler) as Map
String[] testVal = fields.get("value")
log.info("测试 ${testVal}")
}