<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Bean-Validation on Devops Monk</title><link>https://devops-monk.com/tags/bean-validation/</link><description>Recent content in Bean-Validation on Devops Monk</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 03 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://devops-monk.com/tags/bean-validation/index.xml" rel="self" type="application/rss+xml"/><item><title>Bean Validation: @Valid, Custom Validators, and Error Messages</title><link>https://devops-monk.com/tutorials/spring-boot/spring-boot-bean-validation/</link><pubDate>Sun, 03 May 2026 00:00:00 +0000</pubDate><guid>https://devops-monk.com/tutorials/spring-boot/spring-boot-bean-validation/</guid><description>Every request that enters your API needs validation. Without it, invalid data propagates through your application and produces confusing errors deep in the stack. This article covers how to validate data at the API boundary using Jakarta Bean Validation.
Setup &amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt; &amp;lt;artifactId&amp;gt;spring-boot-starter-validation&amp;lt;/artifactId&amp;gt; &amp;lt;/dependency&amp;gt; This includes Hibernate Validator — the reference implementation of Jakarta Bean Validation 3.0.
Built-in Constraints Annotate fields in your DTO with constraint annotations:
public record CreateOrderRequest( @NotNull(message = &amp;#34;Customer ID is required&amp;#34;) UUID customerId, @NotEmpty(message = &amp;#34;Order must contain at least one item&amp;#34;) @Size(max = 50, message = &amp;#34;Order cannot have more than {max} items&amp;#34;) List&amp;lt;@Valid OrderItemRequest&amp;gt; items, @Valid ShippingAddressRequest shippingAddress, @Size(max = 20, message = &amp;#34;Promo code cannot exceed {max} characters&amp;#34;) String promoCode ) {} public record OrderItemRequest( @NotNull UUID productId, @Positive(message = &amp;#34;Quantity must be positive&amp;#34;) @Max(value = 999, message = &amp;#34;Cannot order more than {value} units of a product&amp;#34;) int quantity, @NotNull @Positive BigDecimal unitPrice ) {} public record ShippingAddressRequest( @NotBlank(message = &amp;#34;Address line 1 is required&amp;#34;) @Size(max = 100) String line1, @Size(max = 100) String line2, @NotBlank @Size(max = 50) String city, @NotBlank @Pattern(regexp = &amp;#34;[A-Z]{2}&amp;#34;, message = &amp;#34;Country must be a 2-letter ISO code&amp;#34;) String country, @NotBlank @Pattern(regexp = &amp;#34;\\w{3,10}&amp;#34;, message = &amp;#34;Invalid postal code format&amp;#34;) String postalCode ) {} Common Constraint Annotations Annotation Validates @NotNull Value is not null @NotEmpty String/collection not null and not empty @NotBlank String not null, not empty, not just whitespace @Size(min, max) String length or collection size @Min(value) Number ≥ value @Max(value) Number ≤ value @Positive Number &amp;gt; 0 @PositiveOrZero Number ≥ 0 @Negative Number &amp;lt; 0 @Pattern(regexp) String matches regex @Email Valid email format @Past Date is in the past @Future Date is in the future @DecimalMin(value) Decimal ≥ value (as string) @AssertTrue Boolean is true @AssertFalse Boolean is false Triggering Validation with @Valid Add @Valid to the controller parameter to trigger validation:</description></item></channel></rss>