Skip to content

Quarkus Validation

DodaTech 1 min read

In this tutorial, you'll learn about Fix Quarkus Bean Validation Not Working. We cover key concepts, practical examples, and best practices.

The Problem

Constraint validation annotations like @NotBlank are ignored on REST endpoints.

Quick Fix

Add validation dependency

Wrong:

<!-- No validation -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

Output:

No validation

Right:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

Output:

Validation enabled

Use @Valid on endpoint

Wrong:

@POST
public Response create(Product p) { } // No @Valid

Output:

Invalid data passes

Right:

@POST
public Response create(@Valid Product p) { }

Output:

Validation triggered

Handle validation errors

Wrong:

// No exception handler
@POST
public Response create(@Valid Product p) { }

Output:

Default error response (422)

Right:

@POST
public Response create(@Valid Product p) {
    return Response.ok(p).build();
}
// Or use ExceptionMapper<ValidationException>

Output:

Custom error handling

Prevention

  • Add quarkus-hibernate-validator dependency
  • Add @Valid to method parameters
  • Use ExceptionMapper for custom validation error responses

Common Mistakes with validation

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world QUARKUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Does Quarkus support method-level validation?

Yes, use @javax.validation annotations on method parameters and return values.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro