Backend Development 6 min read

Parameter Validation in the Controller Layer Using Hibernate Validator

This article explains how to cleanly perform parameter validation in Java backend applications by placing business‑independent checks in the Controller layer with Hibernate Validator and optional custom annotations, while business‑related checks belong to the Service layer.

Top Architect
Top Architect
Top Architect
Parameter Validation in the Controller Layer Using Hibernate Validator

When submitting code for review, a colleague pointed out a code‑style issue: parameter validation should be placed in the Controller layer. The article discusses the proper placement of validation logic between Controller and Service layers.

Generally, validation unrelated to business rules belongs in the Controller, while business‑related validation resides 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>

Typical Annotations (shown in the accompanying image).

To use it, annotate the Controller method with @Validated or @Valid . Both work similarly; @Validated offers grouping and ordering features.

Define an Entity

public class DataSetSaveVO {
    // Unique identifier must not be blank
    @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;

    // Data set name must be alphanumeric
    @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;

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

The message field specifies the exception message when validation fails.

Controller Method

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

Adding @Valid (or @Validated ) next to the DTO triggers automatic validation.

Using Apache Commons Lang3

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 , and CollectionUtils.isEmpty are demonstrated in the following test code:

//StringUtils.isEmpty
System.out.println(StringUtils.isEmpty(""));  // true
System.out.println(StringUtils.isEmpty("  ")); // false
//StringUtils.isNotEmpty
System.out.println(StringUtils.isNotEmpty("")); // false

//StringUtils.isBlank
System.out.println(StringUtils.isBlank("")); // true
System.out.println(StringUtils.isBlank(" ")); // true
//StringUtils.isNotBlank
System.out.println(StringUtils.isNotBlank(" ")); // false

List
emptyList = new ArrayList<>();
List
nullList = null;
List
notEmptyList = new ArrayList<>();
notEmptyList.add(1);

//CollectionUtils.isEmpty
System.out.println(CollectionUtils.isEmpty(emptyList)); // true
System.out.println(CollectionUtils.isEmpty(nullList)); // true
System.out.println(CollectionUtils.isEmpty(notEmptyList)); // false

//CollectionUtils.isNotEmpty
System.out.println(CollectionUtils.isNotEmpty(emptyList)); // false
System.out.println(CollectionUtils.isNotEmpty(nullList)); // false
System.out.println(CollectionUtils.isNotEmpty(notEmptyList)); // true

Custom Annotations

If built‑in constraints cannot satisfy a specific validation need, you can create custom annotations; see the author’s previous article "Spring Custom Annotations from Beginner to Master" for details.

Finally, the article includes promotional messages and QR‑code images for the author’s architecture community, which are not part of the technical content.

BackendJavaSpringHibernate ValidatorParameter ValidationController
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.