Search⌘ K

Develop the Mock Service

Develop a mock service for saving data to an in-memory database.

The mock service is a Spring JSON based controller that saves the data to an in-memory database.

add function

Let’s start with a simple addition to the database. The design of the in-memory database is based on the Singleton pattern, which is implemented using enum.

The identifier field starts at one and is incremented by one in each addition.

Java
public enum ProductInMemoryDB {
INSTANCE;
private static List < ProductDto > list = new ArrayList < ProductDto > ();
private static Integer lastId = 0;
public Integer getId() {
return ++lastId;
}
public void add(ProductDto productDto) {
productDto.setId(getId());
list.add(productDto);
}
}

Create operation

...