Java / Micronaut Interview questions
How do you implement bean validation in Micronaut?
Micronaut integrates the Jakarta Bean Validation API directly, and true to form, validates using compile-time generated code rather than runtime reflection over annotations.
public class CreateOrderRequest { @NotBlank private String customerId; @Min(1) private int quantity; } @Post public HttpResponse<Order> create(@Valid @Body CreateOrderRequest request) { ... }
Adding @Valid to a controller method parameter triggers validation automatically before the method body runs; a failing request returns a 400 response with details about which constraints failed, without any manual validation code. The same @Validated/@Valid combination works on any bean method, not just controllers, letting you enforce constraints on service-layer methods too.
Because the validation metadata is compiled in, invalid requests are rejected before touching your business logic, keeping validation and business rules cleanly separated.
More Related questions...