Spring & Java - Serializing a Hierarchical Tree Structure

I am working on adapting the angular-ui-tree unlimited nested module to use server side serialization and persistence.

I now have a GET job and an existing rendering of a nested tree structure. However, the part I'm amazed at is about the create and update operations (POST and PUT).

Given that the JSON structure is on the side of this page, and where each node is a JSON representative of the domain model below, how can I save and update the data?

@Table(name = "categories")
public class Category implements Serializable{

  @Id
  @Column(name = "category_id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;

  @Column
  private String category;

  @ManyToOne(cascade = CascadeType.PERSIST)
  private Category parent;

  @OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, orphanRemoval = true)
  private List<Category> categoryList = new ArrayList<>();

  //getters, setters

}

      

I have an existing controller installed here:

@RequestMapping(value = "/rest/category", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<Category> createCategory(@RequestBody Category category){

}

      

but it only works for objects of the same category where the incoming JSON follows the property view Category

. My alternative is to have the entire JSON structure sent to this controller on create / update operation and update both persistence and tree structure.

I am stuck on how to get this data in the first place. Are there existing libraries or serializers that can be adapted for this use case?

+3


source to share





All Articles