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.

Programmer DD
Programmer DD
Programmer DD
Mastering Parameter Validation in Spring Boot: Hibernate Validator Best Practices

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));  // true

Custom Annotations

If built‑in annotations cannot satisfy specific validation requirements, you can create custom validation annotations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootHibernate ValidatorParameter ValidationControllerService
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.