Package dev.akif.crud

Class CRUDController<I extends Serializable,E extends CRUDEntity<I,E>,M extends CRUDModel<I>,D extends CRUDDTO<I>,CM extends CRUDCreateModel<I,E>,UM extends CRUDUpdateModel<I,E>,CD extends CRUDCreateDTO<I,E,CM>,UD extends CRUDUpdateDTO<I,E,UM>,R extends CRUDRepository<I,E>,S extends CRUDService<I,M,E,CM,UM,R>>

java.lang.Object
dev.akif.crud.CRUDController<I,E,M,D,CM,UM,CD,UD,R,S>
Type Parameters:
I - Id type of the data
E - Entity type of the data
M - Model type of the data
D - DTO type of the data
CM - Create model type of the data
UM - Update model type of the data
CD - Create DTO type of the data
UD - Update DTO type of the data
R - Repository type of the data
S - Service type of the data
Direct Known Subclasses:
SimpleController, SimplerController, SimplestController

@Validated public abstract class CRUDController<I extends Serializable,E extends CRUDEntity<I,E>,M extends CRUDModel<I>,D extends CRUDDTO<I>,CM extends CRUDCreateModel<I,E>,UM extends CRUDUpdateModel<I,E>,CD extends CRUDCreateDTO<I,E,CM>,UD extends CRUDUpdateDTO<I,E,UM>,R extends CRUDRepository<I,E>,S extends CRUDService<I,M,E,CM,UM,R>> extends Object
Base implementation of a CRUD controller for API layer

For some Foo data that have Long ids, a FooController defined as


 @RestController
 @RequestMapping("/foo")
 public class FooController
     extends CRUDController<
         Long,
         FooDTO,
         Foo,
         FooEntity,
         CreateFoo,
         UpdateFoo,
         CreateFooDTO,
         UpdateFooDTO,
         FooRepository,
         FooService> {}
 
automatically implements

 @PostMapping("/")
 public FooDTO create(CreateFooDTO createDTO);

 @GetMapping("/")
 public Paged<FooDTO> getAll(int page, int perPage);

 @GetMapping("/{id}")
 public FooDTO get(Long id);

 @PutMapping("/{id}")
 public FooDTO update(Long id, UpdateFooDTO updateDTO);

 @DeleteMapping("/{id}")
 public void delete(Long id);
 
 

This is meant to be extended from a @RestController class, ideally also with a @RequestMapping with some path prefix for the endpoints.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final S
    Service dependency of this controller
    protected final String
    Type name of the data this controller manages
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    CRUDController(String type, S service)
    Constructor to provide type name and dependencies to this controller
  • Method Summary

    Modifier and Type
    Method
    Description
    create(CD createDTO)
    Default implementation for creating a new entity from given create DTO
    void
    delete(I id)
    Default implementation for deleting an entity with given id
    get(I id)
    Default implementation for getting an entity with given id
    getAll(int page, int perPage)
    Default implementation for listing entities with given pagination
    protected abstract D
    toDTO(M model)
    Mapper to convert from model to DTO
    update(I id, UD updateDTO)
    Default implementation for updating an entity with given id with given update DTO data

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • type

      protected final String type
      Type name of the data this controller manages
    • service

      protected final S extends CRUDService<I,M,E,CM,UM,R> service
      Service dependency of this controller
  • Constructor Details

    • CRUDController

      protected CRUDController(String type, S service)
      Constructor to provide type name and dependencies to this controller
      Parameters:
      type - Type name of the data this controller manages
      service - Service dependency of this controller
  • Method Details

    • toDTO

      protected abstract D toDTO(M model)
      Mapper to convert from model to DTO
      Parameters:
      model - Model to convert
      Returns:
      DTO built from given model
    • create

      @PostMapping(consumes="application/json", produces="application/json") @ResponseStatus(code=CREATED) public D create(@RequestBody CD createDTO)
      Default implementation for creating a new entity from given create DTO
      Parameters:
      createDTO - Create DTO containing data of the entity to create
      Returns:
      DTO of the created entity
    • getAll

      @GetMapping(produces="application/json") public Paged<D> getAll(@RequestParam(name="page",required=false,defaultValue="0") int page, @RequestParam(name="perPage",required=false,defaultValue="20") int perPage)
      Default implementation for listing entities with given pagination
      Parameters:
      page - Number of the 0-based page of entities to list
      perPage - Number of entities to list per page
      Returns:
      Paged of DTOs of entities
    • get

      @GetMapping(path="/{id}", produces="application/json") public D get(@PathVariable("id") I id)
      Default implementation for getting an entity with given id
      Parameters:
      id - Id of the entity
      Returns:
      DTO of the entity with given id
    • update

      @PutMapping(path="/{id}", consumes="application/json", produces="application/json") public D update(@PathVariable("id") I id, @RequestBody UD updateDTO)
      Default implementation for updating an entity with given id with given update DTO data
      Parameters:
      id - Id of the entity to update
      updateDTO - Update DTO containing data to be updated
      Returns:
      DTO of the updated entity
    • delete

      @DeleteMapping("/{id}") @ResponseStatus(NO_CONTENT) public void delete(@PathVariable("id") I id)
      Default implementation for deleting an entity with given id
      Parameters:
      id - Id of the entity to delete