代码
Category category = new Category();
category.setName("test1");
Category category2 = new Category();
category2.setName("test2");
category.setParent(category2);
BrandCategoryDTO brandCategoryDTO1 = new BrandCategoryDTO(category);
System.out.println(JSONObject.toJSONString(brandCategoryDTO1));
public BrandCategoryDTO(Category category) {
this.name = category.getName();
this.id = category.getId();
if (category.getParent() != null) {
this.category = new BrandCategoryDTO(category.getParent());
}
}
最后输出 {"name":"test1"}。预想的结果是将嵌套对象也输出出来。
用 debug 可以明确看到嵌套对象是存在于 dto 对象之中的,但是不知道为什么反序列化成 json 就不行了。
现在实际项目的情况是,把 dto 通过 spring boot 框架直接返回给前端以后,前端看不到里面的嵌套对象,很奇怪。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.