/spring-web-initializr

Spring Web Initializr is a library that helps you easily create Web Apps with Spring Boot.

Primary LanguageJavaMIT LicenseMIT

Spring Web Initializr

build tests
code-coverage-92%

release-2.0.0 jdk-8 spring-boot-2.0.0.RELEASE MIT licensed

Spring Web Initializr (will be referenced Swi from now on) is a library that helps you easily create Web Apps with Spring Boot.
It was initially developed in order to support the Swip (Spring Web Initializr Plugin) built for IntelliJ IDEA, but was extended beyond that usage and can be obviously used independently.

TL;DR

<dependency>
    <groupId>io.github.orpolyzos</groupId>
    <artifactId>spring-web-initializr</artifactId>
    <version>2.0.0</version>
</dependency>

Fully working examples can be found at Swi(p) Demo

Table of Contents

Description

Swi is providing implementations for the Create, Read, Update & Delete (CRUD) operations of an Entity (ResourcePersistable)

  • CRUD REST API
    • exposing directly the ResourcePersistable
    • exposing a DTO instead of the ResourcePersistable
  • CRUD MVC API (ServerSide Rendering using a compatible Template Engine of your choice)
    • exposing directly the ResourcePersistable
    • exposing a DTO instead of the ResourcePersistable

Generic Type Parameters

  • R stands for the class of the ResourcePersistable, the one annotated with @Entity (e.g. User)
  • I stands for the class of the primary key of the ResourcePersistable, the one annotated with @Id (e.g. Long)
  • D stands for the class of the DTO that is going to be exposed instead of the ResourcePersistable (e.g. UserDto)

ResourcePersistable<I>

  • Should be implemented by the corresponding Entity (e.g. User)
  • There is only a single method to be implemented
    • I getRpId(); - Should return the primary key field

RpService<R extends ResourcePersistable<I>, I extends Serializable, D> extends ResourcePersistableService<D, I>

  • It is responsible for the communication with the corresponding CrudRepository<R,I>
  • Should be implemented by the @Service for the corresponding ResourcePersistable (e.g. UserService)
  • There are 3 methods to be implemented
    • CrudRepository<R, I> getRepository(); - Should return the corresponding CrudRepository<R,I> (e.g. UserRepository)
    • Function<R, D> getEntityToDtoConverter(); - Should provide a way for the ResourcePersistable to be converted to the DTO
    • Function<D, R> getDtoToEntityConverter(); - Should provide a way for the DTO to be converted to the ResourcePersistable

NoDtoRpService<R extends ResourcePersistable<I>, I extends Serializable> extends RpService<R, I, R>

  • NoDtoRpService should be used if we want to expose directly the ResourcePersistable
  • Everything from RpService applies to NoDtoRpService, with the difference of 2 already implemented methods
    • default Function<R, R> getEntityToDtoConverter() { return Function.identity(); }
    • default Function<R, R> getDtoToEntityConverter() { return Function.identity(); }

RpRestController<D, I extends Serializable> extends ResourcePersistableRestController<D, I>

  • It is responsible for the communication with the corresponding ResourcePersistableService<R, I>
  • Provides a REST API for the CRUD operations of the ResourcePersistable
  • Should be implemented by the @RestController for the corresponding ResourcePersistable (e.g. UserRestController)
  • There is only a single method to be implemented
    • ResourcePersistableService<D, I> getService(); - Should return the corresponding ResourcePersistableService<D,I> (e.g. UserService)

RpViewController<D, I extends Serializable> extends ResourcePersistableViewController<D, I>

  • It is responsible for the communication with the corresponding ResourcePersistableService<R, I, D>
  • Provides a MVC API for the CRUD operations of the ResourcePersistable
  • Should be implemented by the @Controller for the corresponding ResourcePersistable (e.g. UserController)
  • There is only a single method to be implemented
    • ResourcePersistableService<D, I> getService(); - Should return the corresponding ResourcePersistableService<D,I> (e.g. UserService)

Examples

Contributors

Orestes Polyzos
Orestes Polyzos

To contribute to Spring Web Initializr, follow the instructions in our contributing guide

License

Spring Web Initializr is licensed under the MIT license.
Copyright © 2019, Orestes Polyzos