java 动态设置 注解值_动态修改注解(annotation)值

项目中用到了 @JsonIgnore 注解,因为类中有个详细信息, 这个详细信息在返给前端列表时用不到,只在查看详情时才会用到。所以详情字段加上了@JsonIgnore,它的默认值是true.

所以在查看详情时,还要给前端返回这个详情字段。就要动态将@JsonIgnore设置成false。这个是通过反射完成的。

因为查看详情通常情况下,只返回一个pojo,所以在这里用反射,不会影响系统的性能。

假设 一个类, Detail

public class Detail implementsSerializable {

...

@JsonIgnoreprivateString detail;

...

}

现在要将@JsonIgnore的值改为false

publicDetail getDetail(String id) {

Detail detail=repository.findOne(id);try{Field detailField = message.getClass().getDeclaredField("detail");if (detailField != null) {

detailField.setAccessible(true);

JsonIgnore annotation= detailField.getAnnotation(JsonIgnore.class);if (annotation != null) {

InvocationHandler ih=Proxy.getInvocationHandler(annotation);

Field memberValuesField= ih.getClass().getDeclaredField("memberValues");

memberValuesField.setAccessible(true);

Map memberValues=(Map)memberValuesField.get(ih);

memberValues.put("value", false); // set value to false

}

}

}catch(Exception e) {

e.printStackTrace();

}returnDetail;

}

还有这个:

com.alibaba

fastjson

1.2.33

参考文章:  https://segmentfault.com/a/1190000011213222

2018/6/3: 补充一下

因为所有对象共有一个class对象,所以在将详情字段设为可见后,再查看列表,详情也是可见的。 最终的处理结果就是再建一个一样的类,但详情字段没有@JsonIgnore. 比如AnotherDetail

也就是说,不能用上面说的修改注解来解决这个问题。但这个技术还是有其他的应用。

public class AnotherDetail {

...

private String detail;

...

}

列表用Detail,  这样列表就没有详情字段。

在需要详情的地方用AnotherDetail, 然后将Detail一个个赋给AnotherDetail.

Detail detail = db.getDetail(...);  // JsonIgnore只是控制返给前端的json, 从数据库取出时,detail字段是有值的。

AnotherDetail ad  = new AnotherDetail();

...

ad.setDetail(detail.getDetail());

然后将ad返给需要的函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值