https://github.com/manuelarte/spring-utils
🛠️ Spring Boot utilities java library. Contains a series of utilities to be used with Spring Boot v3
https://github.com/manuelarte/spring-utils
library spring-boot
Last synced: 2 months ago
JSON representation
🛠️ Spring Boot utilities java library. Contains a series of utilities to be used with Spring Boot v3
- Host: GitHub
- URL: https://github.com/manuelarte/spring-utils
- Owner: manuelarte
- Created: 2025-01-17T15:13:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-03-31T07:08:55.000Z (3 months ago)
- Last Synced: 2026-03-31T09:29:06.792Z (3 months ago)
- Topics: library, spring-boot
- Language: Java
- Homepage:
- Size: 222 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 🛠️ 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](https://spring.io/projects/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](#example-project) section.
### CrupRepository
Extension for the [Spring Data Repository](https://docs.spring.io/spring-data/jpa/reference/index.html) to allow **partial updates**.
#### Prerequisites
- The entity needs to have a single field with `@Id` attribute
#### Example
```java
@Entity
public class DocumentEntity {
@Id
private final Long id;
private final String name;
private final String surname;
...
}
@Repository
public interface DocumentEntityRepository extends CrpudRepository {}
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.
```java
@Validated
public class DocumentController {
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity findOne(
@PathVariable @Exists(DocumentEntity.class) final String id) {
return ResponseEntity.ok(documentService.findOne(id));
}
}
@Repository
public interface DocumentEntityRepository extends CrudRepository {}
```
### @New, @PartialUpdate, @Update Validation Groups
Helper annotations to be used in your DTOs to be used
as [validation groups](https://jakarta.ee/specifications/bean-validation/).
#### 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:
```java
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 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 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 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
```java
@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
```java
@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:
```java
@FromAndToDate(paramIndexes={x,y})
```
where `x` and `y` are the indexes of the parameters to be checked.
## Example Project
In the [_example](_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