Coder Social home page Coder Social logo

base-crud-rest-service's Introduction

Base CRUD REST service

This Spring Boot service implements base CRUD operations and try to provide these operations for your entities in declarative way.

Implemented operations:

  1. Create
  2. Update
  3. Delete
  4. Get one
  5. Get all

For example:

POST /users
PATCH /users/1
DELETE /users/1
GET /users/1
GET /users

To use these operations with your entity you have perform the following steps:

  1. Inherit your entity from LongIdEntity (or first create your own base entity - see LongIdEntity as example):

    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Entity
    @Table(name = "users")
    @DynamicInsert
    @DynamicUpdate
    @Cache(usage = READ_WRITE, region = "user")
    public class User extends LongIdEntity {
    
        @Column(nullable = false, length = 32)
        private String name;
    }
  2. Create three DTOs: 'create request', 'update request' and 'response', based on CreateRequest, UpdateRequest and BaseResponse<ID> respectively:

    @Data
    @EqualsAndHashCode(callSuper = false)
    public class UserCreateRequest extends CreateRequest {
        @NotBlank private String name;
    }
    
    @Data
    @EqualsAndHashCode(callSuper = false)
    public class UserUpdateRequest extends UpdateRequest {
        private String name;
    }
    
    @Getter
    @Setter
    public class UserResponse extends BaseResponse<Long> {
        private String name;
    }
  3. Create a repository for your entity, based on BaseRepo<ID, T>

    public interface UserRepo extends BaseRepo<Long, User> {
    }
  4. Create a mapper, based on BaseMapper, which will perform mapping between your entity and DTOs:

    @Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    public abstract class UserMapper implements BaseMapper<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> {
    }
  5. Create a service for your entity. based on AbstractBaseService:

    @Service
    public class UserService extends AbstractBaseService<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> {
    
        public UserService(UserRepo repo, UserMapper mapper) {
            super(repo, mapper);
        }
    }
  6. And create a REST controller, based on BaseController:

    @RestController
    @RequestMapping("users")
    public class UserController extends BaseController<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> {
    
        public UserController(UserService service) {
            super(service);
        }
    
        @PostMapping
        @Override
        protected ResponseEntity<?> create(@Valid @RequestBody UserCreateRequest request) {
            return super.create(request);
        }
    
        @PatchMapping("/{id}")
        @Override
        protected ResponseEntity<?> update(@PathVariable("id") Long id, @Valid @RequestBody UserUpdateRequest request) {
            return super.update(id, request);
        }
    
        @DeleteMapping("/{id}")
        @Override
        protected ResponseEntity<?> delete(@PathVariable("id") Long id) {
            return super.delete(id);
        }
    
        @GetMapping
        @Override
        protected ResponseEntity<?> getAll(Pageable pageable) {
            return super.getAll(pageable);
        }
    
        @GetMapping("/{id}")
        @Override
        protected ResponseEntity<?> getOne(@PathVariable("id") Long id) {
            return super.getOne(id);
        }
    }

Then you will be able to perform all CRUD operations with your entity, for example:

GET /users/1
{
  "name": "user1"
}

You can implement more complex entities, which have associations with over ones - see package io.github.cepr0.demo.user as example. To reduce a pressure to the database, the project uses Ehcache 3 as Hibernate 2nd level cache. You can add respective entries to the 'resources/ehcache.xml' file for your entities.

Project uses MapStruct to generate mappers code automatically. But for complex entities you can make additional codding (see package io.github.cepr0.demo.user as example).

Project uses log4jdbc to log JDBC queries and their results (see 'pom.xml' for 'log4jdbc-spring-boot-starter' dependency and corresponding settings in the 'resources/application.yml'). It also logs Hibernate statistic which you can turn off in 'resources/application.yml'.

Project uses H2 database. You can replace it to another RDBMS - the project does not use specific SQL dialect, only Spring Data JPA query methods.

Project uses Java 11.

base-crud-rest-service's People

Contributors

cepr0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.