Skip to content
Marten Prieß edited this page Aug 14, 2019 · 2 revisions

The main classes and helpers are explained below

AbstractCrudController

Provides an abstract implementation of a simple CRUD controller. Handles between Entity, Read and Write Models/Dtos. Provided endpoints:

  • [GET] /
    • Used to page within all entities
    • Optional Query Parameter
      • pageSize (max items per page - default 25)
      • page (page to start - default 1)
      • sort (could be given multiple times)
        • layout is propertyName for example id
        • optionally you can add the direction: id,desc
    • Return: PageableResult that looks like
      • totalElements (total items found)
      • totalPages (amount of pages with given pageSize)
      • pageSize (used pageSize)
      • content (list of items)
  • [GET] /{id}
    • Used to fetch on single entity
    • Return: ReadDto of entity
  • [POST] /
    • Create a new entity with the use of the WriteDto
    • Activated javax validation
    • Return: ReadDto of entity
  • [PUT] /{id}
    • Update an already existing entity with the use of WriteDto
    • Activated javax validation
    • Return: ReadDto of entity
  • [DELETE] /{id}
    • Delete an already existing entity
    • Return: Void

The controller based on simple spring-data PagingAndSortingRepository and a converter interface EntityReadWriteConverter that maps between Entity, Read and Write. Mainly all needed transformations could be done automatically with mapstruct

defaultSort() is a method you should overwrite in order to provide a valid sorting for pageable result.

AbstractCrudChildController

Is an extension of the "normal" crud-controller. It introduce a parent/child context and offers a bit modified endpoints:

  • [GET] /{partentId}/child
    • Used to page within all entities that are linked to the given parent
    • Optional Query Parameter same as in the normal Crud
    • Return: PageableResult
  • [GET] /{partentId}/child/{id}
    • Used to fetch on single entity
    • Return: ReadDto of entity
  • [POST] /{partentId}/child
    • Create a new child entity with the use of the WriteDto
    • Activated javax validation
    • Return: ReadDto of entity
  • [PUT] /{partentId}/child/{id}
    • Update an already existing child entity with the use of WriteDto
    • Activated javax validation
    • Return: ReadDto of entity
  • [DELETE] /{partentId}/child/{id}
    • Delete an already existing child entity
    • Return: Void

To use this abstract controller you need to implement 3 methods in order to get it working

  • getEntity(parentId, id)
    • should find the entity with check of fitting parentId + id
  • findAllByParentId(parentId, pageable)
    • should return a page of all entities that fit to the given parentId and use of pageable
  • netEntity(parentId, writeData)
    • should create a new entity and take care to store the relation with parent

defaultSort() is a method you should overwrite in order to provide a valid sorting for pageable result.

BaseController

Is an interface that provides some default methods. Mainly some converted of queryParameters are implemented:

  • parsePageRequest(parms, defaultSort)
    • converts an MultiValueMap to a pageable (find for PageableResults)
  • parseInteger(params, key, defaultValue)
    • checks if the given queryParametr (key=name of it) is existing - if not defaultValue will get returned
  • parseLong(params, key, defaultValue)
  • parseBoolean(params, key, defaultValue)
    • allows for true: true,1,yes,on
  • parseLocalDate(params, key, defaultValue)
    • checks in the following order: YYYY-MM-DD, d/MM/yyyy, d.MM.yyyy
  • parseLocalTime(params, key, defaultValue)
    • format ISO_LOCAL_TIME
  • parseLocalDateTime(params, key, defaultValue)
    • format ISO_LOCAL_DATE_TIME

EntityReadWriteConverter

Interface that handles converting between Entity, Read and Write. Mainly used by the AbstractCrudController.

  • fromEntity(entity)
    • convert an entity to ReadDto
  • newEntity(write)
    • create new entity by given write
  • updateEntityFromEdit(write,entity)
    • update existing entity by given WriteDto
    • should not change entitys id, creationDate etc. for example

Contains a default implementation of converting List of Entiy to List of Read.

Configuration

By the autoconfiguration feature of spring some beans will get configured:

  • LocalResolver

  • Used to detect which language the user requests

  • Registers the AcceptHeaderLocaleResolver (that detects the language by the HEADER-Parameter Accecpted-Language)

  • could be disabled with property: locale.resolver.enabled=false

  • default language is en could be changed with property: locale.resolver.default=de

  • BadRequestExceptionHandler

    • Converts the BadRequestException (RuntimeException) to a 400 error-response with code and message within
    • could be disabled with property: handler.badRequest.enabled=false
  • BeanValidationExceptionHandler

    • Converts the javax bean valitions to a bad request with detailed information which property of the bean has some invalidation. example layout:

    • {
        "status": 400,
        "message": "invalid form",
        "fields": {
      		"email": "must be a well-formed email address",
        	"birthDate": "must not be null"
      	}
      }
    • could be disabled with property: handler.beanValidation.enabled=false

Clone this wiki locally