使用 Jackson 和 Json-View 排除 json 中的字段用户类ScreenInfoPojo 类运行代码结果预期成果问题

2022-09-04 22:30:00

我正在使用json-view根据我的需要创建一个动态json,这是一个很棒的库,我现在正在使用这个库一段时间。最近我遇到了一个问题,我的一个用例,让我先放置我的代码

用户类

public class User {

    private String name;
    private String emailId;
    private String mobileNo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

}

ScreenInfoPojo 类

public class ScreenInfoPojo {

    private Long id;
    private String name;
    private ScreenInfoPojo parentScreen;
    private User createdBy;
    private User lastUpdatedBy;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ScreenInfoPojo getParentScreen() {
        return parentScreen;
    }

    public void setParentScreen(ScreenInfoPojo parentScreen) {
        this.parentScreen = parentScreen;
    }



    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public User getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(User lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    } 

运行代码

public class TestMain {
    public static void main(String[] args) throws JsonProcessingException {
        User user=new User();
        user.setName("ABC");
        user.setEmailId("dev@abc123.com");
        user.setMobileNo("123456789");

        ScreenInfoPojo screen1=new ScreenInfoPojo();
        screen1.setId(1l);
        screen1.setName("Screen1");
        screen1.setCreatedBy(user);
        screen1.setLastUpdatedBy(user);

        ScreenInfoPojo screen2=new ScreenInfoPojo();
        screen2.setId(2l);
        screen2.setName("Screen2");
        screen2.setParentScreen(Screen1);
        screen2.setCreatedBy(user);
        screen2.setLastUpdatedBy(user);

        ScreenInfoPojo screen3=new ScreenInfoPojo();
        screen3.setId(3l);
        screen3.setName("Screen3");
        screen3.setParentScreen(Screen2);
        screen3.setCreatedBy(user);
        screen3.setLastUpdatedBy(user);

        ScreenInfoPojo screen4=new ScreenInfoPojo();
        screen4.setId(4l);
        screen4.setName("Screen4");
        screen4.setParentScreen(Screen3);
        screen4.setCreatedBy(user);
        screen4.setLastUpdatedBy(user);

        List<ScreenInfoPojo> screens=new ArrayList<>();
        screens.add(screen1);
        screens.add(screen2);
        screens.add(screen3);
        screens.add(screen4);

        ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());
        String json = mapper.writeValueAsString(JsonView.with(screens).onClass(ScreenInfoPojo.class, Match.match()
                .exclude("*")
                .include("id","name","createdBy.name","lastUpdatedBy.mobileNo","parentScreen.id")));
        System.out.println("json"+json);
    }    

结果

[{
    "id": 1,
    "name": "Screen1",
    "parentScreen": null,
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 2,
    "name": "Screen2",
    "parentScreen": {
        "id": 1,
        "name": "Screen1",
        "parentScreen": null,
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 3,
    "name": "Screen3",
    "parentScreen": {
        "id": 2,
        "name": "Screen2",
        "parentScreen": {
            "id": 1,
            "name": "Screen1",
            "parentScreen": null,
            "createdBy": {},
            "lastUpdatedBy": {}
        },
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 4,
    "name": "Screen4",
    "parentScreen": {
        "id": 3,
        "name": "Screen3",
        "parentScreen": {
            "id": 2,
            "name": "Screen2",
            "parentScreen": {
                "id": 1,
                "name": "Screen1",
                "parentScreen": null,
                "createdBy": {},
                "lastUpdatedBy": {}
            },
            "createdBy": {},
            "lastUpdatedBy": {}
        },
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}]

预期成果

[{
    "id": 1,
    "name": "Screen1",
    "parentScreen": null,
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 2,
    "name": "Screen2",
    "parentScreen": {
        "id": 1
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 3,
    "name": "Screen3",
    "parentScreen": {
        "id": 2
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 4,
    "name": "Screen4",
    "parentScreen": {
        "id": 3
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}]

问题

在我的用例中,我有一个类ScreenInfoPojo,它引用了与parternScreen相同的类,我正在尝试获取父级(“parentScreen.id”)的特定字段/字段,状态我正在获取我在子/目标对象(“id”,“name”,“createdBy.name”,“lastUpdatedBy.mobileNo”,“parentScreen.id”)上定义的所有字段,并且父响应再次递归!我观察到的一件事是,只有在类有自己的引用的情况下才会发生这种情况,我将User类引用作为两个不同的字段创建BylastUpdatedBy,并尝试分别获取“name”“mobileNo”工作得很好。

任何解决这个问题的建议都将非常有帮助!!!!

谢谢


答案 1

是的。include 子句不适用于对同一类的引用。

你能做到吗?

根据 github 指令从源代码构建,从源代码编译

更新函数 JsonViewSerializer.JsonWriter.fieldAllowed

找到:

        if(match == null) {
            match = this.currentMatch;
        } else {
            prefix = "";
        }

和注释 else 子句

        if(match == null) {
            match = this.currentMatch;
        } else {
            //prefix = "";
        }

您将获得预期的结果。但。我不知道它将如何影响另一个过滤器。

要获得更多控制权,您可以将属性添加到 JsonView 类。

例如:

在 JsonView 中添加:

private boolean ignorePathIfClassRegistered = true;

     public boolean isIgnorePathIfClassRegistered() {
            return ignorePathIfClassRegistered;
        }

        public JsonView1<T>  setIgnorePathIfClassRegistered(boolean ignorePathIfClassRegistered) {
            this.ignorePathIfClassRegistered = ignorePathIfClassRegistered;
            return this;
        }

在 JsonViewSerializer.JsonWriter.fieldAllowed 函数中,如果子句重写为:

        if(match == null) {
            match = this.currentMatch;
        } else {
            if (result.isIgnorePathIfClassRegistered())
            prefix = "";
        }

您可以在示例中使用它,例如:

JsonView<List<ScreenInfoPojo>> viwevedObject = JsonView
        .with(screens)
        .onClass(ScreenInfoPojo.class,
                Match.match()
                        .exclude("*")
                        .include("id","name")
                        .include("createdBy.name")
                        .include("lastUpdatedBy.mobileNo")
                        .include("parentScreen.id"))
        .setIgnorePathIfClassRegistered(false);

ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

String json = mapper.writeValueAsString(viwevedObject);

答案 2

您只需在 json 响应中不需要的字段上使用 jackson 注释@jsonignore

我不知道您是否可以在代码上使用任何注释。如果是这样,这是没用的。