Search code examples
helidon

How to use Bean Validation in a Helidon App?


I'm trying to create a simple Rest Resource using Helidon-MP but for some reason it doesn't work when I add the bean validation annotations on my method.

@POST
public Response generatePlan(@Valid @ValidPlan JsonObject payload) {
   // some logic here
}

Is this the expected behavior? Or Should I add some dependency or configuration?

I tried to find something on the documentation, but I couldn't.

Thanks


I ended up doing as @LairdNelson answered and added the following dependencies in my pom.xml:

<dependency>
     <groupId>org.hibernate.validator</groupId>
     <artifactId>hibernate-validator</artifactId>
     <version>6.1.5.Final</version>
</dependency>
<dependency>
     <groupId>org.hibernate.validator</groupId>
     <artifactId>hibernate-validator-cdi</artifactId>
     <version>6.1.5.Final</version>
</dependency>
<dependency>
     <groupId>jakarta.el</groupId>
     <artifactId>jakarta.el-api</artifactId>
</dependency>
<dependency>
     <groupId>org.glassfish</groupId>
     <artifactId>jakarta.el</artifactId>
</dependency>

Solution

  • Bean validation is not part of the MicroProfile set of specifications, so by default it is not part of Helidon MP, which is a MicroProfile implementation.

    You may of course add an implementation of Bean Validation on your compile or runtime classpath provided it is a CDI portable extension (which is how you add arbitrary capabilities to any MicroProfile-compliant implementation). One such portable extension is the Hibernate Validator-backed one. There may certainly be others.