0
inernetの作成ヘルプ リストがあるカテゴリエンティティがあり、jsonでこのツリーを送信する必要がありますが、daoを使用してすべてのカテゴリと私のjsonはこのようになりますリストの変換方法リスト<Entite >のリストをtreeSetに変換
[
{
"id": 1,
"title": "Phones & Accessories",
"products": [],
"subcategories": [
{
"id": 2,
"title": "Mobile Phones",
"products": [],
"subcategories": []
},
{
"id": 3,
"title": "Phone Bags & Cases",
"products": [],
"subcategories": []
},
{
"id": 4,
"title": "Mobile Phone Accessories",
"products": [],
"subcategories": []
},
{
"id": 5,
"title": "Mobile Phone Parts",
"products": [],
"subcategories": []
}
]
},
{
"id": 2,
"title": "Mobile Phones",
"products": [],
"subcategories": []
},
{
"id": 3,
"title": "Phone Bags & Cases",
"products": [],
"subcategories": []
},
カテゴリは重複していますので、私の知る限り、リストを返す必要はなく、コントローラのメソッドでtreeSetを返す必要があります。しかし、私は私のJSONが
@Controller
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping("/categories")
public @ResponseBody TreeSet<Category> getCategoriesTree(){
List<Category> list = categoryService.findAll();
TreeSet<Category> categoryTree = new TreeSet<>(list);
return categoryTree;
}
@RequestMapping("/categorieslist")
public @ResponseBody List<Category> getCategoriesList(){
List<Category> list = categoryService.findAll();
TreeSet<Category> categorytree = new TreeSet<>(list);
return list;
}
}
コントローラのマッピングエンティティ
@Entity
@Getter
@Setter
public class Category implements Comparable<Category> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String title;
@OneToMany
@JoinTable(name = "category_product", joinColumns = { @JoinColumn(name = "category_id") }, inverseJoinColumns = { @JoinColumn(name = "product_id") })
private Set<Product> products;
@OneToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "category_subcategory", joinColumns = { @JoinColumn(name = "category_id") }, inverseJoinColumns = { @JoinColumn(name = "subcategory_id") })
private List<Category> subcategories;
@Override
public int compareTo(Category o) {
if (o.getSubcategories().contains(this)) {
return 0;
} else return 1;
}
}
ツリーセットはあなたのケースとは何の共通点もありません - ツリーはソートされた要素を格納する基本的なメカニズムを表します。しかし、あなたは何らかの木構造を意味していますが、この概念を台無しにしないでください。 – bsiamionau
リスト list = categoryService.findAll(); TreeSet categoryTree =新しいTreeSet <>(リスト); –
qwd412
@Override public int compareTo(カテゴリo){ if(this.getSubcategories()。contains(o))は0を返します。 else return 1; } – qwd412