Backend Development 6 min read

Parameter Validation in Spring: Controller vs Service Layer, Hibernate Validator, and Commons‑Lang3

This article explains how to place parameter validation in the appropriate Spring layer, demonstrates using Hibernate Validator and commons‑lang3 for concise validation annotations, provides code examples for entity definitions and controller methods, and mentions creating custom validation annotations when needed.

Architecture Digest
Architecture Digest
Architecture Digest
Parameter Validation in Spring: Controller vs Service Layer, Hibernate Validator, and Commons‑Lang3

When reviewing code, a colleague suggested that parameter validation should be placed in the controller layer. This article explains the distinction between controller and service layers for validation and demonstrates how to write elegant validation code using existing tools.

Controller layer vs Service layer – Generally, validation unrelated to business logic belongs in the controller, while business‑related validation belongs in the service.

Using Hibernate Validator

Add the Maven dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.1.Final</version>
</dependency>

Common annotations such as @NotBlank, @Pattern, @Length are used on entity fields. Example entity:

public class DataSetSaveVO {
    @NotBlank(message = "user uuid is empty")
    @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;

    @Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers")
    @Length(max = 48, message = "file name too long")
    @NotBlank(message = "file name is empty")
    private String name;

    @Length(max = 256, message = "data set description length over 256 byte")
    @NotBlank(message = "data set description is null")
    private String description;
}

In the controller, add @Valid or @Validated to the request body:

@PostMapping
public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {
    return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));
}

Using commons‑lang3

Add the Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Common utility methods such as StringUtils.isEmpty, StringUtils.isBlank, CollectionUtils.isEmpty are demonstrated in the test code:

//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
emptyList = new ArrayList<>();
List
nullList = null;
List
notEmptyList = new ArrayList<>();
notEmptyList.add(1);
System.out.println(CollectionUtils.isEmpty(emptyList)); // true
System.out.println(CollectionUtils.isEmpty(nullList)); // true
System.out.println(CollectionUtils.isEmpty(notEmptyList)); // false

If built‑in annotations are insufficient, a custom validation annotation can be created (refer to the author’s previous article on Spring custom annotations).

The article ends with a promotional block inviting readers to join a Java technology learning group.

Javabackend developmentSpringHibernate ValidatorParameter ValidationControllerCommons Lang3
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.