Skip to content

Spring Boot Integration

Add rulii-bean-validation beside rulii-spring 2.3.0 and it becomes the application’s Bean Validation provider, wired into the Spring rulii context. Adding the dependency is the switch; there is nothing else to configure.

<dependency>
<groupId>org.rulii</groupId>
<artifactId>rulii-bean-validation</artifactId>
<version>1.1.0</version>
</dependency>

It takes the place of Hibernate Validator, even when Hibernate Validator is still on the classpath. To turn it off while the dependency stays, set rulii.validation.enabled=false; Staying on Hibernate Validator has the details. Without the dependency rulii-spring leaves Bean Validation alone.

The library is not a certified implementation and does not run the TCK. The specification coverage page lists every known difference, and the migration guide says what a project coming from Hibernate Validator will notice.

With this library and Spring Boot’s validation module on the classpath (what spring-boot-starter-validation brings), the auto-configuration org.rulii.spring.validation.BeanValidationConfig runs before Boot’s own validation auto-configuration and defines the defaultValidator bean Boot would otherwise define: a LocalValidatorFactoryBean pinned to RuliiValidationProvider. The provider is therefore selected whatever else is on the classpath, Hibernate Validator included.

  • The Validator bean is rulii’s. Everything that validates through it, @Valid on controller arguments, @Validated services through Boot’s method validation post-processor, does so unchanged.
  • @Validate names are bean names. The provider’s configuration receives the Spring RuleRegistry, so a rule, rule set or flow bean is attachable by its bean name.
  • The Spring rulii context applies. The Spring RuleContextOptions (converters, message resolver, tracer, object factory) are the options validation runs with; ConstraintValidators are created as Spring beans with injection.
  • Mappers and extractors are beans. Every ConstraintMapper and ValueExtractor bean is registered on the provider.
  • Boot’s customizers still apply. ValidationConfigurationCustomizer beans run after the rulii wiring.
  • The unwrapped BeanValidator is a bean too, for code that wants validateIn or the materialized flows without unwrapping.
@Service
@Validated
public class OrderService {
private final BeanValidator validator;
public OrderService(BeanValidator validator) {
this.validator = validator;
}
public Receipt place(@Valid Order order) { // method validation, through the rulii provider
...
}
public RuleViolations check(RuleContext context, Order order) {
return validator.validateIn(context, order); // inside a rulii context you own
}
}
@Component("orderRules") // a @Validate("orderRules") name
public class OrderRules { ... }
Property Default Meaning
rulii.validation.enabled true when rulii-bean-validation is on the classpath false turns rulii validation off while the dependency stays and keeps Hibernate Validator (see below).
rulii.validation.failFast false Stop at the first violation.
rulii.script.defaultLanguage none The scripting language for @Expr and script rules that name none; read from the Spring Environment as rulii reads the system property of the same name.
rulii.validation.failFast=true
rulii.script.defaultLanguage=jakarta-el

An @Expr or @Bind(expr = ...) in Jakarta EL can be written with either of EL’s delimiters, #{...} or ${...}. They evaluate the same. The examples throughout these pages use #{...}:

@ExprDefaults(lang = "jakarta-el")
public class Trip {
public LocalDate start;
@Expr(value = "#{start == null || end.isAfter(start)}", errorCode = "trip.end")
public LocalDate end;
}

rulii-spring resolves Spring property placeholders (${some.property}) in script text before it is compiled, but from rulii 2.3.0 it leaves Jakarta EL scripts as written, because ${...} is EL’s own syntax.

  • A property placeholder inside a jakarta-el script is therefore not resolved. Pass the value in as a binding instead.
  • ${value} and ${validatedValue} in a message are templates, not scripts, and work as documented.
  • Expressions in other languages (js, java, SpEL as el) still have their ${property} placeholders resolved.

From rulii-spring 2.2. rulii-bean-validation no longer comes with rulii-spring. Add the dependency to keep validating with rulii; without it the application goes back to the validator Spring Boot sets up.

From rulii-spring 2.1 or earlier, or adding rulii validation for the first time. A Boot application that validated with Hibernate Validator validates with rulii once the dependency is added, with nothing else changed. Violations, paths and messages are meant to come out the same; the migration guide lists the differences.

Taking Hibernate Validator out is optional. While it is on the classpath, a library that calls Validation.buildDefaultValidatorFactory() itself gets whichever provider comes first, and JPA keeps using it (see JPA). To take it out, exclude hibernate-validator from the validation starter:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>

Keep the starter itself: it brings Boot’s validation wiring and an EL implementation, which ${...} in messages and @Expr in Jakarta EL need.

JPA is not affected. Spring Boot does not hand its Validator to JPA; Hibernate builds one of its own. While Hibernate Validator is on the classpath, JPA goes on validating entities with it, whatever the dependency order. So @Validate and @Expr on an entity are not checked at persist time: validate the entity through the Validator bean before saving it. An application that takes Hibernate Validator out has one provider left, and JPA validates with rulii.

To keep Hibernate Validator while this library is on the classpath, set rulii.validation.enabled=false. HibernateValidatorConfig then defines the defaultValidator bean pinned to Hibernate Validator, with Boot’s message interpolator and customizers, so the opt-out is deterministic although two providers are on the classpath. Without Hibernate Validator on the classpath the opt-out leaves the bean to Boot, which bootstraps the one provider there is, rulii’s, without the Spring rulii wiring.

rulii.validation.enabled=false

Both configurations are @ConditionalOnMissingBean(Validator.class): define a Validator bean of your own and neither runs. To keep the rulii wiring and still customise, prefer a ValidationConfigurationCustomizer bean, which receives the RuliiConfiguration after the Spring context has been applied:

@Bean
ValidationConfigurationCustomizer failFastInProduction(Environment env) {
return configuration -> {
if (configuration instanceof RuliiConfiguration rulii && env.acceptsProfiles(Profiles.of("prod"))) {
rulii.failFast(true);
}
};
}

The rulii explorer has a Beans section for applications that validate with rulii-spring’s validator: with rulii.explorer.beans.enabled=true it lists every validated class member by member, opens the rule built for each constraint, draws the cascades between beans and the @Validate rules they run, and flags a @Validate name nothing is registered under.