[JAVA] Check date correlation with Spring Boot

Overview

Summarize the method (correlation check) of comparing multiple items with Spring Boot and performing validation.

code

First, the entity class that receives the date input from the user.

package com.sample.Form;

import com.sample.Annotation.DayCheck;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;

@Data
@DayCheck //(1)
public class SearchLogForm {
  @NotNull(message = "Please put the date^^") //(2)
  private LocalDate startDate;

  @NotNull(message = "Please put the date^^")
  private LocalDate endDate;
}

Commentary (1)-> Add your own annotation to be created later to the whole class. (2)-> Since it is meaningless if the date is not entered in the first place, @NotNull annotation is added so as not to allow Null.

Next, the annotation class to be given to the entity.

package com.sample.Annotation;

import com.sample.Validation.DayCheckValidation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = DayCheckValidation.class) //(1)
@Target(ElementType.TYPE) //(2)
@Retention(RetentionPolicy.RUNTIME)
public @interface DayCheck {
  String message() default "Enter the end date after the start date^^";

  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};
}

Commentary (1)-> Specify the validation check class to be created later. (2)-> When validating multiple items, specify TYPE because it is given to the class itself.

Finally, create a validation check class.

package com.sample.Validation;

import com.sample.Annotation.DayCheck;
import com.sample.Form.SearchLogForm;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DayCheckValidation implements ConstraintValidator<DayCheck, SearchLogForm> { //(1)

  @Override
  public boolean isValid(SearchLogForm value, ConstraintValidatorContext context) {
    if (value.getStartDate() == null || value.getEndDate() == null) { //(2)
      return false;
    }
    return value.getStartDate().isBefore(value.getEndDate()) ? true : false; //(3)
  }
}

Commentary (1)-> In <> of Constraint Validator, specify the entity class to annotate (check) the annotation created above before the comma and after it. (2)-> If the date is null, a nullpo will occur, so check for null. (3)-> Compare dates. Implemented to return false if the end date is before the start date and true if it is later.

Execution result

1611130067549.jpg If you set the start date to a date before the end date ...

1611130001999.jpg I was able to check it properly.

References

・ Https://terasolunaorg.github.io/guideline/public_review/ArchitectureInDetail/Validation.html#validation-correlation-check ・ Https://itneko.com/kotlin-validate-correlation/

Recommended Posts

Check date correlation with Spring Boot
Download with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
[Introduction to Spring Boot] Form validation check
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Form class validation test with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Spring Boot environment construction with Docker (January 2021 version)
Create a website with Spring Boot + Gradle (jdk1.8.x)
Configure Spring Boot application with maven multi module
Test controller with Mock MVC in Spring Boot