├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── javatechie │ │ ├── CustomValidationApplication.java │ │ ├── controller │ │ └── EmployeeController.java │ │ ├── dto │ │ └── Employee.java │ │ ├── handler │ │ └── ApplicationExceptionHandler.java │ │ ├── service │ │ └── EmployeeService.java │ │ └── valiadtion │ │ ├── EmployeeTypeValidator.java │ │ └── ValidateEmployeeType.java └── resources │ └── application.properties └── test └── java └── com └── javatechie └── CustomValidationApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # custom-validation -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.7.3 9 | 10 | 11 | com.javatechie 12 | custom-validation 13 | 0.0.1-SNAPSHOT 14 | custom-validation 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-validation 27 | 28 | 29 | org.projectlombok 30 | lombok 31 | true 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/CustomValidationApplication.java: -------------------------------------------------------------------------------- 1 | package com.javatechie; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class CustomValidationApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(CustomValidationApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.controller; 2 | 3 | import com.javatechie.dto.Employee; 4 | import com.javatechie.service.EmployeeService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import javax.validation.Valid; 12 | 13 | @RestController 14 | @RequestMapping("/employees") 15 | public class EmployeeController { 16 | 17 | @Autowired 18 | private EmployeeService service; 19 | 20 | @PostMapping 21 | public Employee addNewEmployeeToSystem(@RequestBody @Valid Employee employee){ 22 | return service.addNewEmployee(employee); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/dto/Employee.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import com.javatechie.valiadtion.ValidateEmployeeType; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import javax.validation.constraints.*; 10 | import java.util.Date; 11 | 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class Employee { 16 | //it should auto generate 17 | private int empId; 18 | @NotBlank(message = "firstName shouldn't be null or empty") 19 | private String firstName; 20 | @NotBlank(message = "lastName shouldn't be null or empty") 21 | private String lastName; 22 | @Past(message = "start shouldn't be before current date") 23 | @JsonFormat(pattern = "dd-MM-yyyy") 24 | private Date doj; 25 | @NotNull(message = "department shouldn't be null") 26 | @NotEmpty(message = "department shouldn't be empty") 27 | private String dept; 28 | @Email(message = "invalid email id") 29 | private String email; 30 | 31 | //custom annotation 32 | @ValidateEmployeeType 33 | private String employeeType; //permanent or vendor or contractual 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/handler/ApplicationExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.handler; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.MethodArgumentNotValidException; 5 | import org.springframework.web.bind.annotation.ExceptionHandler; 6 | import org.springframework.web.bind.annotation.ResponseStatus; 7 | import org.springframework.web.bind.annotation.RestControllerAdvice; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | @RestControllerAdvice 13 | public class ApplicationExceptionHandler { 14 | 15 | 16 | @ExceptionHandler(MethodArgumentNotValidException.class) 17 | @ResponseStatus(HttpStatus.BAD_REQUEST) 18 | public Map handleMethodArgumentException(MethodArgumentNotValidException exception) { 19 | Map errorMap = new HashMap<>(); 20 | exception.getBindingResult().getFieldErrors().forEach(error -> { 21 | errorMap.put(error.getField(), error.getDefaultMessage()); 22 | }); 23 | 24 | return errorMap; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.service; 2 | 3 | import com.javatechie.dto.Employee; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.Random; 7 | 8 | @Service 9 | public class EmployeeService { 10 | 11 | public Employee addNewEmployee(Employee employee){ 12 | employee.setEmpId(new Random().nextInt(68736432)); 13 | //add employee to database 14 | return employee; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/valiadtion/EmployeeTypeValidator.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.valiadtion; 2 | 3 | import javax.validation.ConstraintValidator; 4 | import javax.validation.ConstraintValidatorContext; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | public class EmployeeTypeValidator implements ConstraintValidator { 9 | @Override 10 | public boolean isValid(String employeeType, ConstraintValidatorContext constraintValidatorContext) { 11 | List employeeTypes = Arrays.asList("Permanent", "vendor"); 12 | return employeeTypes.contains(employeeType); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/valiadtion/ValidateEmployeeType.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.valiadtion; 2 | 3 | import javax.validation.Constraint; 4 | import javax.validation.Payload; 5 | import java.lang.annotation.*; 6 | 7 | @Target({ElementType.FIELD,ElementType.PARAMETER}) 8 | @Retention(RetentionPolicy.RUNTIME) 9 | @Documented 10 | @Constraint(validatedBy = EmployeeTypeValidator.class) 11 | public @interface ValidateEmployeeType { 12 | 13 | public String message() default "Invalid employeeType: It should be either Permanent Or vendor"; 14 | 15 | Class[] groups() default {}; 16 | 17 | Class[] payload() default {}; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/test/java/com/javatechie/CustomValidationApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javatechie; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class CustomValidationApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------