A Java 17 utility library for basic parameter validation using Spring AOP and annotations.
This library provides automatic parameter validation through AOP (Aspect-Oriented Programming) using annotations. It supports configurable return types when validation fails, making it suitable for various use cases. This is the Java 17 version with updated dependencies and modern Java features.
- Annotation-based validation: Simple annotations for common validation scenarios
- AOP integration: Automatic validation without boilerplate code
- Configurable return types: Choose between throwing exceptions, returning null, or returning empty collections
- Spring Boot 3.x integration: Works with the latest Spring Boot versions
- Java 17 compatibility: Leverages modern Java features and performance improvements
- Java 17
- Spring Boot 3.5.5 (AOP)
- Apache Commons Lang3 & Collections
- Bean Validation API
- Hibernate Validator 8.x
- Lombok
<dependency>
<groupId>com.goody.utils</groupId>
<artifactId>basic-check-java-17</artifactId>
<version>1.0.0</version>
</dependency>@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@Service
public class UserService {
@BasicCheck
public void updateUser(@CheckLong Long userId, @CheckString String name) {
// Method implementation
// Validation happens automatically before execution
}
@BasicCheck(returnType = BasicCheck.ReturnType.EMPTY)
public List<User> findUsers(@CheckCollection List<Long> userIds) {
// Returns empty list if userIds is null or empty
return userRepository.findByIds(userIds);
}
}@BasicCheck: Enables validation for the methodreturnType: Configure return behavior on validation failureEXCEPTION(default): ThrowsIllegalArgumentExceptionNULL: ReturnsnullEMPTY: Returns appropriate empty collection/Optional
@CheckNull: Validates parameter is not null@CheckString: Validates string is not null or blank@CheckLong: Validates Long is not null and >= 0@CheckCollection: Validates Collection is not null or empty@CheckMap: Validates Map is not null or empty@CheckObject: Validates object using Bean Validation (JSR-303/Jakarta)
@BasicCheck
public void processData(@CheckLong Long id, @CheckString String name) {
// Throws IllegalArgumentException if id is null/negative or name is null/blank
}@BasicCheck(returnType = BasicCheck.ReturnType.EMPTY)
public List<Item> getItems(@CheckCollection List<Long> ids) {
// Returns Collections.emptyList() if ids is null or empty
return itemService.findByIds(ids);
}@BasicCheck(returnType = BasicCheck.ReturnType.NULL)
public void updateItem(@CheckNull Item item) {
// Returns null (void method behavior) if item is null
itemRepository.save(item);
}@BasicCheck
public void saveUser(@CheckObject UserDTO user) {
// Validates user object using Bean Validation annotations
// UserDTO should have @NotNull, @Size, etc. annotations
}| Return Type | Method Return Type | Behavior |
|---|---|---|
EXCEPTION |
Any | Throws IllegalArgumentException |
NULL |
Any | Returns null |
EMPTY |
List<T> |
Returns Collections.emptyList() |
EMPTY |
Set<T> |
Returns Collections.emptySet() |
EMPTY |
Map<K,V> |
Returns Collections.emptyMap() |
EMPTY |
Optional<T> |
Returns Optional.empty() |
This version takes advantage of:
- Performance improvements in Java 17 JVM
- Updated dependencies compatible with Spring Boot 3.x
- Jakarta EE namespace (jakarta.* instead of javax.*)
- Modern validation framework (Hibernate Validator 8.x)
# Compile the project
mvn clean compile
# Run tests
mvn test
# Package the jar
mvn package
# Install to local repository
mvn installIf migrating from the Java 8 version:
- Update Java version to 17+
- Update Spring Boot to 3.x
- Change javax.validation imports to jakarta.validation
- Update dependency versions as shown in pom.xml
This project is licensed under the MIT License.
- Goody - Initial work - Version 1.0, 2023/01/30