[JAVA] Spring validation and error code

Data binding annotation

Annotation used in the domain class.

Annotation Description Example
@NumberFormat Converts a string in the specified format to a numeric type. @NumberFormat(pattern="#,###")
@DateTimeFormat Converts a string in the specified format to a date type. @DateTimeFormat(pattern="yyyy/MM/dd")

Form.java


import org.springframework.format.annotation.DateTimeFormat;

public class SignupForm {

  @DateTimeFormat(pattern="yyyy/MM/dd")
  private Date birthday;
}

Data binding error message

Create messages.properties in the resouces directory and write as follows. There is no need for ** double quotation marks ** after the equal.

pattern Description method Description example
typeMismatch..= typeMismatch.signupForm.age=Please enter a number
typeMismatch.= typeMismatch.age=Please enter a number
typeMismatch.= typeMismatch.int=Please enter a number

List of validation annotations

Annotation used in the domain class.

Classification Annotation Description
Bean Validation @NotNull Check that it is not null
@NotEmpty Check that the string or Collection is not null or empty
@NutBlank Check that the string is not only null, empty string, whitespace
@Max Check if it is less than or equal to the specified value
@Min Check if it is more than the specified value
@Size Check if the length of the string and the size of the Collection are within the specified range
@AssertTrue Check if true
@AssertFale Check if false
@Pattern Check if it matches the specified regular expression
@Email Check if the string is in the form of an email address
Hibernate Validator @Range Check if the value is within the specified range
@Length Check if the length of the string is within the specified range
@CreditCardNumber Check if the string is in credit card number format
@URL Check if the string is in URL format

Differences between NotNull NotEmpty Not Blank

An error will occur at ** NG **.

Annotation null Empty string Blank
@NotNull NG OK OK
@NotEmpty NG NG OK
@NotBlank NG NG NG

Form.java


//When the classification is Bean Validation
// javax.validation.Import from constraints
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Email;
import javax.validation.constraints.Pattern;

//Classification is Hibernate Validator
// org.hibernate.validator.Import from constraints
import org.hibernate.validator.constraints.Length;

public class SignupForm {
  
  //Required input, email address format
  @NotBlank
  @Email
  private String userId;

  @Length(min=4, max=8)
  @Pattern(regexp="^[a-zA-Z0-9]+$")
  private String password;
}

Validation error message

Add to the same messages.properties as the data binding error message

pattern Description method Description example
..= NotBlank.signupForm.userId=Please enter your user ID
.= NotBlank.uerId=Please enter your user ID
.= NotBlank.java.lang.Sting ≒ required input
= NotBlank=Required input
= require_check=Required

Recommended Posts

Spring validation and error code
About error when implementing spring validation
Error in implementation when implementing Spring validation
Spring Boot Whitelabel Error Page and JSON Response
Self-made Validation with Spring
About Spring Framework context error
Common processing and error processing springmvc
Signing and validation using java.security.Provider
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Error in Spring database connection
Spring single item validation test
Spring Boot validation message changes
bitFlyer Lightning error code collection
Spring validation was important in the order of Form and BindingResult
Read and generate QR code [Android]
Java's Angry Code 5 Selections and Amendments
Spring Boot application code review points
Spring Boot + PostgreSQL error resolution method
OVal (annotation-based validation FW) code example
Java Spring environment in vs Code
Write test code in Spring Boot
[Rails] Unexpected validation error in devise
Java 15 implementation and VS Code preferences
Spring Boot programming with VS Code
Writing code with classes and instances
Java 9 new features and sample code
Get validation results with Spring Boot
Flowing interface trial and error memo
[Rails] Validation settings and Japanese localization
Spring profile function, and Spring Boot application.properties
Commit failed exit code 1 error resolution