SoFunction
Updated on 2025-04-09

SpringBoot integrated Aviator to implement parameter verification example code

1. Brief description

In actual development, parameter verification is an important measure to ensure system stability and data reliability. Conventional verification methods include annotation verification of JSR 303 and custom verification logic, but for some complex conditional judgments, using these methods will make the code redundant and difficult to maintain. Aviator is a high-performance expression engine that can simplify complex logical judgments and improve the flexibility of parameter verification. This article will explain how to integrate Aviator in Spring Boot and leverage it to enable flexible parameter verification.

2. Advantages

Aviator is a high-performance, lightweight Java expression engine open sourced by the Chinese team. It supports rich data types and operators, and can handle complex logical operations and custom functions. Compared with other expression engines (such as MVEL and Groovy), Aviator is lighter and has better performance, especially suitable for dynamic calculations and parameter verification in high concurrency scenarios.

  • High performance: Bytecode generation and optimization are used internally, and the operation speed is fast.
  • Flexibility: Supports dynamic writing of expressions to facilitate the expansion of complex verification rules.
  • Lightweight: No need for a lot of dependencies, easy to integrate into Spring Boot projects.

3. Integrate Aviator

3.1 Project Dependence

First, make sure you add the Aviator dependency to your Spring Boot project. Include the following dependencies in the file:

<dependency>
    <groupId></groupId>
    <artifactId>aviator</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

3.2 Defining custom annotations

Next, we define a custom annotation @AviatorValidation to mark the method parameters that need to be checked.

import ;
import ;
import ;
import ;

@Retention()
@Target()
public @interface AviatorValidation {
    String expression(); // Aviator expression    String errorMessage() default "The parameters do not meet the requirements"; // error message}

3.3 Create AOP facets

We then create an AOP section ValidationAspect, which is used to intercept method calls and perform parameter verification logic.

import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

@Aspect
@Component
public class ValidationAspect {

    @Around("execution(* ..*(.., @AviatorValidation (*), ..))")
    public Object validate(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) ();
        Method method = ();

        // Get the @AviatorCheck annotation of the method        AviatorValidation aviatorCheck = ();
        if (aviatorCheck != null) {
            // Prepare parameter values            Object[] args = ();
            String[] parameterNames = ();
            Map&lt;String, Object&gt; env = new HashMap&lt;&gt;();

            for (int i = 0; i &lt; ; i++) {
                (parameterNames[i], args[i]);
            }

            // Use Aviator to verify expression            Boolean result = (Boolean) ((), env);
            if (!result) {
                throw new IllegalArgumentException(());
            }
        }

        return ();  // Continue to execute the target method    }
}

3.4 Using annotations on methods

Use the @AviatorValidation annotation where verification is required, specifying the verification expression and error message.

 @GetMapping("/test")
 @AviatorValidation(expression = "param1 > 0 && param2 < 100", errorMessage = "param1 should be > 0 and param2 < 100")
 public String test(@RequestParam int param1, @RequestParam int param2) {
     return "Validation passed!";
 }

In the above example, we define different parameter verification logic for the method by annotating @AviatorValidation. Before the method is executed, it will intercept and check whether the conditions are met. Otherwise, an exception will be thrown and the specified error message will be returned.

4. Extended functions

  • Multi-parameter support: Multiple parameters of the method can be passed to Aviator through Map for verification.
  • Custom functions: Register Aviator custom functions, such as checking whether the string contains special characters, checking the date format, etc.
  • Configurable expressions: Configure expressions into databases or configuration files to facilitate dynamic modification of verification rules.

5. Summary

By combining Aviator with AOP, we can implement flexible parameter verification logic. This method not only simplifies the writing of verification logic, but also improves the maintainability of the code. As business needs change, it is easy to adapt to new requirements by modifying the verification expression.

In practical applications, more verification rules and complex expressions can be extended to meet diverse business needs. Hopefully this article can provide some inspiration and help for your parameter verification in Spring Boot project!

The above is the detailed content of the example code for SpringBoot integrated Aviator to implement parameter verification. For more information about SpringBoot Aviator parameter verification, please pay attention to my other related articles!