动态修改注解(annotation)值

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

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

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

 

假设 一个类, Detail

public class Detail implements Serializable {
    
    ...

    @JsonIgnore
    private String   detail;
    
        ...
}

 

现在要将@JsonIgnore的值改为false

public Detail 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();
        }
        
        return Detail;
    }

还有这个:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.33</version>
</dependency>

 

参考文章:  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返给需要的函数。

 

posted @ 2018-05-15 11:49  北极熊129  阅读(9219)  评论(4编辑  收藏  举报