Recent comments

Creacion de Bean con validacion:


 
Clase Ejemplo: 
 

package com.resfull.restfull.model;  
 import java.util.List;  
 import javax.validation.constraints.AssertTrue;  
 import javax.validation.constraints.Max;  
 import javax.validation.constraints.Min;  
 import javax.validation.constraints.NotNull;  
 import javax.validation.constraints.Null;  
 import javax.validation.constraints.Pattern;  
 import javax.validation.constraints.Size;  
 import org.springframework.lang.NonNull;  
 import org.springframework.lang.Nullable;  
 import com.fasterxml.jackson.annotation.JsonInclude;  
 import com.fasterxml.jackson.annotation.JsonProperty;  
 import com.fasterxml.jackson.annotation.JsonPropertyOrder;  
 import com.resfull.restfull.entity.Nota;  
 import io.swagger.annotations.ApiModel;  
 import io.swagger.annotations.ApiModelProperty;  
 //incluye solo datos no nulos  
 @JsonInclude(JsonInclude.Include.NON_NULL)  
 //orden de request  
 //Generalmente se utiliza para los response  
 @JsonPropertyOrder({  
   "id",  
   "nombre",  
   "titulo",  
   "contenido",  
   "contador",  
   "notaEspecifica"  
 })  
 @ApiModel("Model Nota")  
 public class MNota {  
   @JsonPropertyOrder("id")  
   @ApiModelProperty(value = "the nota's id", required = true)  
   @Nullable  
   private long id;  
   //valida que el campo sea incluido puedes agregar un mensaje  
   @JsonPropertyOrder("nombre")  
   @Nullable  
   @ApiModelProperty(value = "the nombre", required = true)  
   private String nombre;  
   //Valida que sea requerido y que tenga minimo 1 y maximo 10  
   //Tambien se puede poner que sea o no sea requerido y que cuando sea requerido se valide el tamaño  
   //simplemente agregando la etiqueta @Nullable en lugar de @NotNull  
   @JsonPropertyOrder("titulo")  
   @NotNull  
   @Size(min = 1, max = 10)  
   @ApiModelProperty(value = "the titulo", required = true)  
   private String titulo;  
   @JsonPropertyOrder("contenido")  
   @ApiModelProperty(value = "the contenido", required = true)  
   private String contenido;  
   //El contador puede contenerlo o no, pero si lo contiene tiene que estar entre 18 y 30  
   //tambien te valida si es string  
   @JsonPropertyOrder("contador")  
   @Nullable  
   @Min(8)  
   @Max(30)  
   private Integer contador;  
   //tieen que venir true  
   //@AssertTrue  
   //private boolean isRequired;  
   //@Email(message = "Email should be valid")  
   //private String email;  
   //Valida el dato en el request integer es obligatorio no respeta @Nullable  
   //@Min(value = 18, message = "Age should not be less than 18")  
   //@Max(value = 150, message = "Age should not be greater than 150")  
   //private int contador;  
   //@Nullable  
   //@Pattern(regexp = "[0-9]*")  
   //private String cadenaNumerica;  
   //puede se agregada o no pero si es agregada minimo debe contener un valor y maximo 2  
   @JsonPropertyOrder("notaEspecifica")  
   @Nullable  
   @Size(min = 1, max = 2, message = "user.tags.size")  
   private List<MNotaEspecifica> notaEspecifica;  
   public MNota() {  
   }  
   public MNota(Nota nota) {  
     this.id = nota.getId();  
     this.nombre = nota.getNombre();  
     this.titulo = nota.getTitulo();  
     this.contenido = nota.getContenido();  
   }  
   public MNota(long id, String nombre, String titulo, String contenido) {  
     this.id = id;  
     this.nombre = nombre;  
     this.titulo = titulo;  
     this.contenido = contenido;  
   }  
   public long getId() {  
     return id;  
   }  
   public void setId(long id) {  
     this.id = id;  
   }  
   public String getNombre() {  
     return nombre;  
   }  
   public void setNombre(String nombre) {  
     this.nombre = nombre;  
   }  
   public String getTitulo() {  
     return titulo;  
   }  
   public void setTitulo(String titulo) {  
     this.titulo = titulo;  
   }  
   public String getContenido() {  
     return contenido;  
   }  
   public void setContenido(String contenido) {  
     this.contenido = contenido;  
   }  
   public Integer getContador() {  
     return contador;  
   }  
   public void setContador(Integer contador) {  
     this.contador = contador;  
   }  
   public List<MNotaEspecifica> getNotaEspecifica() {  
     return notaEspecifica;  
   }  
   public void setNotaEspecifica(List<MNotaEspecifica> notaEspecifica) {  
     this.notaEspecifica = notaEspecifica;  
   }  
   @Override  
   public String toString() {  
     return "MNota [id=" + id + ", nombre=" + nombre + ", titulo=" + titulo + ", contenido=" + contenido  
         + ", contador=" + ", notaEspecifica=" + "]";  
   }  
 }  

Validaciones disponibles:

Using Bean Validation Constraints

 

The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean.
Constraints can be built in or user defined. User-defined constraints are called custom constraints. Several built-in constraints are available in the javax.validation.constraints package. Table 23-1 lists all the built-in constraints. See Creating Custom Constraints for information on creating custom constraints.
Table 23-1 Built-In Bean Validation Constraints



@AssertFalse
boolean isUnsupported;
 @AssertTrue
boolean isActive;
 @DecimalMax("30.00")
BigDecimal discount;
 @DecimalMin("5.00")
BigDecimal discount;
 @Digits(integer=6, fraction=2)
BigDecimal price;
@Email
String emailaddress;
 
@FutureOrPresent
Time travelTime;
@Future
Date eventDate;
 
@Max(10)
int quantity;
 @Min(5)
int quantity;
 @Negative
int basementFloor;
@NegativeOrZero
int debtValue; 
@NotBlank
String message;
 @NotEmpty
String message;;
@NotNull
String username; 
@Null
String unusedString;
@Past
Date birthday; 
@PastOrPresent
Date travelDate;
 @Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")
String phoneNumber;
 @Positive
BigDecimal area;
 @PositiveOrZero
int totalGoals;
@Size(min=2, max=240)
String briefMessage; 

No hay comentarios.