Master Elegant Request Parameter Validation in Spring Boot with JSR‑303
This tutorial explains why server‑side request parameter validation is essential, introduces the JSR‑303 Bean Validation standard and its Hibernate Validator implementation, and provides step‑by‑step code examples, Swagger integration tips, and dependency guidance for building robust Spring Boot REST APIs.
Request parameter validation is a common pitfall for beginners; relying only on front‑end checks leaves security holes, and using tangled if/else logic makes maintenance hard.
Therefore server‑side validation is essential and should be elegant, readable, and maintainable.
JSR‑303
JSR‑303 is the Bean Validation specification; Hibernate Validator is its reference implementation and provides built‑in constraints.
Built‑in constraints
Hibernate Validator additional constraints
Using these annotations you can define validation rules for request parameters.
Hands‑on practice
Prepare a Spring Boot 2.x project (or use the example repository).
Step 1 – Add @NotNull
@Data
@ApiModel(description="User entity")
public class User {
@ApiModelProperty("User ID")
private Long id;
@NotNull
@ApiModelProperty("User name")
private String name;
@NotNull
@ApiModelProperty("User age")
private Integer age;
}Step 2 – Add @Valid on controller method
@PostMapping("/")
@ApiOperation(value="Create user", notes="Create user from User object")
public String postUser(@Valid @RequestBody User user) {
users.put(user.getId(), user);
return "success";
}When you POST an empty JSON to localhost:8080/users/ you receive a 400 response with a structured error payload containing timestamp, status, error, and an errors array with field, rejectedValue, defaultMessage, etc.
More complex validation
@Data
@ApiModel(description="User entity")
public class User {
@ApiModelProperty("User ID")
private Long id;
@NotNull
@Size(min=2, max=5)
@ApiModelProperty("User name")
private String name;
@NotNull
@Max(100)
@Min(10)
@ApiModelProperty("User age")
private Integer age;
@NotNull
@Email
@ApiModelProperty("User email")
private String email;
}Posting invalid values yields a 400 response with detailed error messages for each violated constraint.
Swagger documentation
Swagger partially supports JSR‑303 annotations; supported ones include @NotNull, @Max, @Min, @Size, and @Pattern. For unsupported constraints you can add descriptions in @ApiModelProperty.
Dependency note
The spring-boot-starter-validation starter pulls in hibernate-validator. In Spring Boot 2.1 it is already included via spring-boot-starter-web, so an extra dependency may not be required.
Full source code
The complete example can be found in the chapter2-3 directory of the repository:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/tree/2.x
Gitee: https://gitee.com/didispace/SpringBoot-Learning/tree/2.x
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.
