Download - Bean validation 2017
![Page 2: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/2.jpg)
QueesBean Validation
• EspecificacióndesarrolladorporelJavaCommunity Process.
• SufunciónesvalidarJavaBeans sebasaenelusodeanotaciones.
• Bean Validation 1.0JSR303• Bean Validation 1.1JSR349• Bean Validation 2.0JSR380
![Page 3: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/3.jpg)
Escenariodevalidaciones
![Page 4: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/4.jpg)
AnotacionesAnotación Ejemplo@AssertFalse @AssertFalse
boolean isUnsupported;
@AssertTrue @AssertTrueBoolean isActive;
@DecimalMax @DecimalMax("30.00")BigDecimalDiscount;
@DecimalMin @DecimalMin("5.00")BigDecimal discount;
@Digits @Digits(integer=6,fraction=2)BigDecimal price;
@FutureDate @FutureDateeventDate;
@Max @Max(10)int quantity;
![Page 5: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/5.jpg)
AnotacionesAnotación Ejemplo@Min @Min(5)
int quantity;
@NotNull @NotNullString username;
@Null @NullString unusedString;
@Past @PastDatebirthday;
@Pattern @Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")String phoneNumber;
@Size @Size(min=2,max=240)String briefMessage;
![Page 6: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/6.jpg)
EntidadconAnotacionespublic class Clientes implements java.io.Serializable {
@NotNullprivate long cliId;@NotNullprivate TiposDocumentos tiposDocumentos;@NotNull@Size(min = 1, max = 50)private String cliNombre;@NotNull@Size(min = 1, max = 50)private String cliDireccion;@NotNull@Size(min = 1, max = 50)private String cliTelefono;@NotNull@Size(min = 1, max = 50)private String cliMail;
}
![Page 7: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/7.jpg)
Usandoelvalidador
Set<ConstraintViolation<Clientes>> constraintViolations =validator.validate(clientes);log.info(""+constraintViolations.size());
![Page 8: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/8.jpg)
SpringyBean Validation
• Implementacióndejavax.validation.Validator• LocalValidatorFactoryBean implementacióndesarrolladaporelgrupodeSpring
• SepuedeconfigurarenapplicationContext.xml
<bean id="validator"class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
![Page 9: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/9.jpg)
TallerimplementarBeanValidation
• Registraranotacionesenlosentity• ConfigurarelLocalValidatorFactoryBean• Injectar Validator• Ejecutarvalidate• RevisarlalistadeConstraintViolation
![Page 10: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/10.jpg)
HibernateValidator
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.4.1.Final</version>
</dependency>
<dependency><groupId>org.glassfish</groupId><artifactId>javax.el</artifactId><version>3.0.1-b08</version>
</dependency>
![Page 11: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/11.jpg)
applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="validator"class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</beans>
![Page 12: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/12.jpg)
InyectarValidator
import javax.validation.ConstraintViolation;import javax.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;
@Service@Scope("singleton")public class ClientesLogica implements IClientesLogica{
@Autowiredprivate Validator validator;
}
![Page 13: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/13.jpg)
Métodoparavalidar@Autowiredprivate Validator validator;
public void validarClientes(Clientes clientes) throws Exception {try {
Set<ConstraintViolation<Clientes>> constraintViolations = validator.validate(clientes);
if (constraintViolations.size() > 0) {StringBuilder strMessage = new StringBuilder();
for (ConstraintViolation<Clientes> constraintViolation : constraintViolations) {strMessage.append(constraintViolation.getPropertyPath().toString());strMessage.append(" - ");strMessage.append(constraintViolation.getMessage());strMessage.append(". \n");
}
throw new Exception(strMessage.toString());}
} catch (Exception e) {throw e;
}}
![Page 14: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/14.jpg)
Informacióndecontacto
• SitioWeb:www.vortexbird.com• Blog:http://blog.vortexbird.com• Contactovíamail:[email protected]• Direccion:Calle18#118-241Oficina21• Teléfonos:+57- (3164824629)
![Page 15: Bean validation 2017](https://reader031.vdocument.in/reader031/viewer/2022021922/5a657d4d7f8b9af3678b5139/html5/thumbnails/15.jpg)