Skip to content

codedecode25/ValidationAndMapStructDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

ValidationAndMapStructDemo

  1. Dependencies to be added while craeting the project

spring-boot-starter-data-jpa

spring-boot-starter-web

mysql-connector-java

lombok

  1. Application.properties:

server.port=8081

spring.datasource.url= jdbc:mysql://localhost:3306/DemoDB spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

  1. In DBever : create new connection with database name as : localhost and multiple DBs will be available. Now create new DB dd name to applicatio.prop file.

  2. create 5 folders - controller , service, repo, dtos, entities

  3. Create entity Employee with @Id @GeneratedValue(strategy = GenerationType.AUTO)

  4. To remove get, set for dto to entity, use map struct:

org.mapstruct mapstruct 1.4.1.Final
	and a plugin . The mapstruct-processor is used to generate the mapper implementation during the build: : 
	
	<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </path>
    </annotationProcessorPaths>
</configuration>
	Now create a popluator package and an Employee populator interface. Annotate with @Mapper
	Create poplator instance by : EmployeePopulator INSTANCE = Mappers.getMapper(EmployeePopulator.class);
	
	Ignore extra fields using @Mapping(target = "id", ignore = true)
  1. Now for validations use :
org.hibernate.validator hibernate-validator
	When Spring Boot finds an argument annotated with @Valid, it automatically bootstraps the default JSR 380 implementation — Hibernate Validator — and validates the argument.

When the target argument fails to pass the validation, Spring Boot throws a MethodArgumentNotValidException exception.

Now create @controllerAdvice and @ecxception handler to manage 400 BAD request and not so user riendly mesages on postman

@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentNotValidException.class) public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getAllErrors().forEach((error) -> { String fieldName = ((FieldError) error).getField(); String errorMessage = error.getDefaultMessage(); errors.put(fieldName, errorMessage); }); return errors; }

  1. for Making validation at create and updation different we have to create groups for that first create 2 interfaces - craete and update - marker interfaces then In controller changes @valid to @validated and created.class or updatedvalidatior.class based on need Then in POJO add groups{} to add different groups for validation

  2. for Unique email validator do following steps:

Create a custom annotation with

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = { UniqueEmailValidation.class}) public @interface UniqueEmailValidator { //error message public String message() default "Invalid color: must be RED, GREEN or BLUE"; //represents group of constraints public Class<?>[] groups() default {}; //represents additional information about annotation public Class<? extends Payload>[] payload() default {};

}

Create a class UniqueEmailValidation public class UniqueEmailValidation implements ConstraintValidator<UniqueEmailValidator, String> {

@Autowired
EmployeeRepo employeeRepo ;

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
	
	if(employeeRepo.findByEmailAddress(value).size() == 0)
		return true;
	return false;
}

}

Now annotate the DTO with created annotation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages