通过动态@JsonIgnore注释按需延迟加载 [英] On demand lazy loading via dynamic @JsonIgnore annotation

查看:66
本文介绍了通过动态@JsonIgnore注释按需延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西:

@Entity
@Table(name = "myEntity")
public class MyEntity {
  //....

  @Column(name = "content")
  private byte[] content;
  //....
}

问题: 我将MyEntity作为JSON字符串传递给客户端. 但是问题是我有两种类型的客户请求:

PROBLEM: I pass MyEntity to the client as JSON string. But the problem is that I have two types of client's requests:

  1. 我需要通过带有 byte [] content 数组的 MyEntity
  2. 我需要传递MyEntity 而没有 byte []内容数组
  1. I need to pass MyEntity with byte[] content array
  2. I need to pass MyEntity without byte[] content array

在第一种情况下,我不需要@JsonIgnore注释,在第二种情况下-确实需要.

In first case I needn't @JsonIgnore annotation, in second - do need.

问题:

  1. 如何实现动态@JsonIgnore批注?可能吗?
  2. 是否有其他方法可以实现延迟加载?

P.S.据我了解,即使我用延迟加载注释标记了我的 byte [] content 数组,当Jackson将MyEntity解析为JSON字符串时,无论如何都将加载该数组.

P.S. As I understand, even if I mark my byte[] content array with lazy-load annotation, it will be loaded anyway when Jackson will parse MyEntity to JSON-string.

提前谢谢!

推荐答案

您可以使用 Jackson视图.请看下面的例子:

You can use Jackson views. Please, see my below example:

import java.io.IOException;
import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        Entity entity = new Entity();
        entity.setId(100);
        entity.setContent(new byte[] { 1, 2, 3, 4, 5, 6 });

        ObjectMapper objectMapper = new ObjectMapper();

        System.out.println("Generate JSON with basic properties: ");
        System.out.println(objectMapper.writerWithView(View.BasicView.class).writeValueAsString(entity));
        System.out.println("Generate JSON with all properties: ");
        System.out.println(objectMapper.writerWithView(View.ExtendedView.class).writeValueAsString(entity));
    }
}

interface View {

    interface BasicView {
    }

    interface ExtendedView extends BasicView {
    }
}

class Entity {

    @JsonView(View.BasicView.class)
    private int id;

    @JsonView(View.ExtendedView.class)
    private byte[] content;

    public byte[] getContent() {
        System.out.println("Get content: " + Arrays.toString(content));
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public int getId() {
        System.out.println("Get ID: " + id);
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Entity [id=" + id + ", content=" + Arrays.toString(content) + "]";
    }
}

以上程序打印:

Generate JSON with basic properties: 
Get ID: 100
{"id":100}
Generate JSON with all properties: 
Get ID: 100
Get content: [1, 2, 3, 4, 5, 6]
{"id":100,"content":"AQIDBAUG"}

如您所见,在基本视图下,杰克逊不会读取content属性.

As you can see, with basic view Jackson doesn't read content properties.

这篇关于通过动态@JsonIgnore注释按需延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆