Mastering Parameter Validation in Spring Boot: Hibernate Validator Best Practices
This article explains how to perform elegant parameter validation in Spring Boot by distinguishing controller‑level and service‑level checks, introduces Hibernate Validator annotations, demonstrates their usage with code examples, and shows additional utilities like commons‑lang3 and custom validation annotations for robust backend development.
Controller Layer vs Service Layer
General recommendation: validation unrelated to business logic belongs in the Controller, while business‑related validation belongs in the Service.
Common Validation Tools
Using Hibernate Validator
Dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>Common annotations (illustrated in the image below):
Usage: apply @Validated or @Valid on controller methods; both work, but @Validated adds grouping and ordering capabilities.
Annotations are placed on entity fields.
Define an entity with validation annotations:
public class DataSetSaveVO {
// Unique identifier must not be blank
@NotBlank(message = "user uuid is empty")
// User name must be alphanumeric
@Pattern(regexp = "^[a-z0-9]+$", message = "user names can only be alphabetic and numeric")
@Length(max = 48, message = "user uuid length over 48 byte")
private String userUuid;
// Data set name must be alphanumeric
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers")
// File name length limit
@Length(max = 48, message = "file name too long")
// File name must not be blank
@NotBlank(message = "file name is empty")
private String name;
// Description length limit
@Length(max = 256, message = "data set description length over 256 byte")
// Description must not be blank
@NotBlank(message = "data set description is null")
private String description;
}Controller method example:
@PostMapping
public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {
return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));
}Using commons‑lang3
Dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>Common utility methods demonstration:
// StringUtils.isEmpty
System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(" ")); // false
// StringUtils.isBlank
System.out.println(StringUtils.isBlank("")); // true
System.out.println(StringUtils.isBlank(" ")); // true
// CollectionUtils.isEmpty
List<Integer> emptyList = new ArrayList<>();
List<Integer> nullList = null;
System.out.println(CollectionUtils.isEmpty(emptyList)); // true
System.out.println(CollectionUtils.isEmpty(nullList)); // trueCustom Annotations
If built‑in annotations cannot satisfy specific validation requirements, you can create custom validation annotations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
