Skip to content

BasicCheck module is a lightweight parameter validation framework that uses AOP to provide simple, annotation-based validation for method parameters with configurable error handling strategies. This provides a clean, declarative way to handle common parameter validation scenarios without cluttering business logic with boilerplate validation code.

License

Notifications You must be signed in to change notification settings

GOODDAYDAY/basic-check-java-17

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Basic Check Java 17

A Java 17 utility library for basic parameter validation using Spring AOP and annotations.

Overview

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.

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

Dependencies

  • Java 17
  • Spring Boot 3.5.5 (AOP)
  • Apache Commons Lang3 & Collections
  • Bean Validation API
  • Hibernate Validator 8.x
  • Lombok

Quick Start

1. Add the dependency to your Maven project:

<dependency>
  <groupId>com.goody.utils</groupId>
  <artifactId>basic-check-java-17</artifactId>
  <version>1.0.0</version>
</dependency>

2. Enable Spring Boot AOP in your application:

@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. Use annotations in your service methods:

@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);
    }
}

Available Annotations

Method-level Annotation

  • @BasicCheck: Enables validation for the method
    • returnType: Configure return behavior on validation failure
      • EXCEPTION (default): Throws IllegalArgumentException
      • NULL: Returns null
      • EMPTY: Returns appropriate empty collection/Optional

Parameter-level Annotations

  • @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)

Usage Examples

Basic Parameter Validation

@BasicCheck
public void processData(@CheckLong Long id, @CheckString String name) {
    // Throws IllegalArgumentException if id is null/negative or name is null/blank
}

Return Empty Collections

@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);
}

Return Null on Validation Failure

@BasicCheck(returnType = BasicCheck.ReturnType.NULL)
public void updateItem(@CheckNull Item item) {
    // Returns null (void method behavior) if item is null
    itemRepository.save(item);
}

Complex Object Validation

@BasicCheck
public void saveUser(@CheckObject UserDTO user) {
    // Validates user object using Bean Validation annotations
    // UserDTO should have @NotNull, @Size, etc. annotations
}

Return Type Behavior

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()

Java 17 Specific Features

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)

Building and Testing

# Compile the project
mvn clean compile

# Run tests
mvn test

# Package the jar
mvn package

# Install to local repository
mvn install

Migration from Java 8 Version

If migrating from the Java 8 version:

  1. Update Java version to 17+
  2. Update Spring Boot to 3.x
  3. Change javax.validation imports to jakarta.validation
  4. Update dependency versions as shown in pom.xml

License

This project is licensed under the MIT License.

Author

  • Goody - Initial work - Version 1.0, 2023/01/30

About

BasicCheck module is a lightweight parameter validation framework that uses AOP to provide simple, annotation-based validation for method parameters with configurable error handling strategies. This provides a clean, declarative way to handle common parameter validation scenarios without cluttering business logic with boilerplate validation code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages