0

Let's say I have two entities

public class Container {
    
    UUID id;
    
    String name;
    
    @OneToMany
    List<Element> elements
}

and


public class Element {

    UUID id;

    String name;
    
    @ManyToOne
    Container container;

}

What would be the best approach to designing a REST API for these entities? For example should Elements be added to the container via a Container endpoint or via Element endpoint? Or when creating a new container should the Elements be created with the Container in a single request or in a separate request and subsequently added to the Container in another request? Should I have an endpoint to create multiple elements at once?

1
  • 1
    This is opinion - there is no 'best' design. But your options are not mutually exclusive. You can accept a Container endpoint that operates on container DTOs and handle the collection of elements in that DTO how ever you want - even cascading the merge/persist /delete options as required. (ie orphan removal can clean up de-referenced Element instances, but that isn't a good one if you want them to exist on their own or be moved around). You could also have individual element endpoints that accept Element DTOs and change their collections through that
    – Chris
    Commented Jul 14 at 17:48

1 Answer 1

2

It depends on your business need and use case.
If elements are known and should be created on container creation, then it's better to include elements creation at the same API request. You can also pass it as an empty array if no elements to be created for some containers so that you make your API dynamic for both cases. Of course in this case you will also need an element creation API if there is a possibility that elements will not be all added at the time of container creation.

However, if always elements will be added later then create create 2 separate APIs without including elements in the container creation API.

And for the elements creation API it's best practice to always make it an array, and in case you will add only one element, then the array will contain only one item.

2
  • Thank you for the answer. However I'd still like to get some clarification. The most common use case would be to create the elements together with the container. This means that I would have to include all the necessary information for Element creation in the "create container request" i.e. I would have some DTO for Element. Now assume that I want to move some element from one container to another. Thus I would have to send an "update container request" with element DTOs (containing all the information) just to move it from one container to another. It just doesn't feel right to me.
    – Axo
    Commented Jul 14 at 8:22
  • 1
    @Axo So make another endpoint to handle that use case differently in an approach that best suits IT'S usecase. There is no one pattern that is best for everything. You can make an element specific or container specific REST resource that modifies the single element within it without having to pass in a full CollectionDTO all the time ie POST /collection/id/element {addElementJSon} or PUT /collection/id/element {existingElementJSON} or even do so through the /element/{elementid} operation
    – Chris
    Commented Jul 14 at 17:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.