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 dataE
- Entity type of the dataM
- Model type of the dataD
- DTO type of the dataCM
- Create model type of the dataUM
- Update model type of the dataCD
- Create DTO type of the dataUD
- Update DTO type of the dataR
- Repository type of the dataS
- 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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CRUDController
(String type, S service) Constructor to provide type name and dependencies to this controller -
Method Summary
Modifier and TypeMethodDescriptionDefault implementation for creating a new entity from given create DTOvoid
Default implementation for deleting an entity with given idDefault implementation for getting an entity with given idgetAll
(int page, int perPage) Default implementation for listing entities with given paginationprotected abstract D
Mapper to convert from model to DTODefault implementation for updating an entity with given id with given update DTO data
-
Field Details
-
type
Type name of the data this controller manages -
service
Service dependency of this controller
-
-
Constructor Details
-
CRUDController
Constructor to provide type name and dependencies to this controller- Parameters:
type
- Type name of the data this controller managesservice
- Service dependency of this controller
-
-
Method Details
-
toDTO
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 listperPage
- Number of entities to list per page- Returns:
Paged
of DTOs of entities
-
get
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 updateupdateDTO
- Update DTO containing data to be updated- Returns:
- DTO of the updated entity
-
delete
Default implementation for deleting an entity with given id- Parameters:
id
- Id of the entity to delete
-