Skip to content

๐Ÿ› ๏ธ Spring Boot utilities java library. Contains a series of utilities to be used with Spring Boot v3

Notifications You must be signed in to change notification settings

manuelarte/spring-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

41 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ› ๏ธ Spring-Utils library

โฌ‡๏ธ Installation

Add the following dependency in your project to start using the features described below.

Gradle Kotlin
implementation("com.github.manuelarte.spring-utils:{latest-version}")
Gradle Groovy
implementation 'com.github.manuelarte.spring-utils:{latest-version}'

๐Ÿค“ Overview

Some helpful validations and utilities to be used in your Spring Boot application.

Check the features list below.

๐Ÿ“‹ Features

Below you can find a description on the available features in this library, in case you want to see them in a real project check the Example Project section.

CrupRepository

Extension for the Spring Data Repository to allow partial updates.

Prerequisites

  • The entity needs to have a single field with @Id attribute

Example

@Entity
public class DocumentEntity {
    @Id
    private final Long id;
    private final String name;
    private final String surname;
    ...
}

@Repository
public interface DocumentEntityRepository extends CrpudRepository<DocumentEntity, Long> {}

final DocumentEntity saved = repository.save(new DocumentEntity(1, "Manuel", "D"));
final DocumentEntity partialUpdate = new DocumentEntity(null, null, "Doncel");
final DocumentEntity partialUpdated = repository.partialUpdate(1, partialUpdate);
// Result id:1, name: Manuel, surname: Doncel

@Exists Constraint

The @Exists constraint can be used to check if the entity exists before executing the method. It checks whether the repository contains an entity with the specified id. This allows you to abstract the validation logic from the business logic.

Prerequisites

  • The constraint validations need to be executed, for example annotating your Controller with @Validated.
  • The entity/document that is going to be checked needs to have a @Repository bean.
  • The following dependency is needed: implementation("org.springframework.boot:spring-boot-starter-validation")

Example

Here there is an example of a controller that gets a Document by id. The @Exists constrains check whether the id exists before executing the method.

@Validated
public class DocumentController {
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<DocumentEntity> findOne(
    @PathVariable @Exists(DocumentEntity.class) final String id) {
        return ResponseEntity.ok(documentService.findOne(id));
    }
}

@Repository
public interface DocumentEntityRepository extends CrudRepository<DocumentEntity, Long> {}

@New, @PartialUpdate, @Update Validation Groups

Helper annotations to be used in your DTOs to be used as validation groups.

Example

Imagine that you have an entity that you want to allow to be created, updated and partially updated. By using validation groups, we can have the same DTO for the different CRUD endpoints. Here is an example:

public class OneEntityDto {

    @Null(groups = {New.class, PartialUpdate.class})
    @NotNull(groups = Update.class)
    // id is mandatory for the @New and @Update validation group, but not for @PartialUpdate 
    private final Long id;

    @NotEmpty(groups = {New.class, Update.class})
    // firstName can't be empty for @New and @Update, but can be empty for @PartialUpdate
    private final String firstName;
    @NotEmpty(groups = {New.class, Update.class})
    // lastName can't be empty for @New and @Update, but can be empty for @PartialUpdate
    private final String lastName;

}

@RestController
@Validated
public class OneEntityController {
    @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<OneEntity> saveOne(
            @Validated({Default.class, New.class}) @RequestBody final OneEntity saveEntity) {
        // saveEntity will be validated with the @New validation group
    }
    
    @PostMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<OneEntity> updateOne(
        @Validated({Default.class, Update.class}) @RequestBody final OneEntity updateEntity) {
        // updateEntity will be validated with the @Update validation group
    }

    @PatchMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<OneEntity> partialUpdateOne(
            @Validated({Default.class, PartialUpdate.class}) @RequestBody final OneEntity patchEntity) {
        // patchEntity will be validated with the @PartialUpdate validation group
    }
}

FromAndToDate Constraint

@FromAndToDate constraint helps to validate two dates.

Class Level Example

@FromAndToDate
public class EntityExample {
    @FromDate
    private final Date from;
    @ToDate
    private final Date to;
    ...
}

The constraint will check that the field from is lower than to the to parameter

Method Level Example

@FromAndToDate
public void methodExample(final Date from, final Date to) {
    //...
}

The constraint will check that the parameters from and to match from is before than to. By default, the constraint will check the 1st and 2nd parameters indexes. In case they are in a different index it should be set like this:

@FromAndToDate(paramIndexes={x,y})

where x and y are the indexes of the parameters to be checked.

Example Project

In the _example folder there is a Spring Boot project showing the features available in this library.

๐Ÿค Contributing

Feel free to create a PR or suggest improvements or ideas.

Publish

To publish a new version use:

./gradlew publish

About

๐Ÿ› ๏ธ Spring Boot utilities java library. Contains a series of utilities to be used with Spring Boot v3

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages