代码
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 框架直接返回给前端以后,前端看不到里面的嵌套对象,很奇怪。