Java SpringBoot 실체 클래스 데이터 자동 검증

4754 단어
package demo.dto;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;

public class ProductDto implements Serializable {
    @NotEmpty(message = "        ")
    @Length(min = 2, max = 10, message = "         {min} - {max}   ")
    private String userName;

    @NotEmpty(message = "        ")
    private String password;

    @NotEmpty(message = "          ")
    private String realName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName == null ? null : realName.trim();
    }
    /**
     *
     @NotEmpty,@NotNull @NotBlank   
     1 @NotEmpty :   null, Size>0

     2 @NotNull:   null,    empty,  Size   

     3 @NotBlank:   String,   null trim()  size>0
     *
     @NotNull
                  null,        。

     @Null
                 null,        。

     @Size
           ,           5    。

     @Size(min = 1, max = 5)
     private String name;
     1
     2
     @Max
           ,        19,        。
     @Max(value = 19)
     private Integer age;
     1
     2
     @Min
       ,             ,       。

     @AssertFalse
          false ,      。

     @AssertTrue
          true ,      。

     @DecimalMax
             。

     @DecimalMax(value = "12.35")
     private double money;
     1
     2
     @DecimalMin
             。

     @Digits
                             。

     @Digits(integer = 2, fraction = 2)
     private double money;
     1
     2
     @Future
                  ,        。
     @Future
     private Date date;
     1
     2
     @Past
                  ,        。

     @Pattern
                      。

     @Pattern(regexp = "[abc]")
     private String name;
     */

}

  
package demo.entity;

import java.io.Serializable;

public class Product implements Serializable {
    private Integer id;

    private String userName;

    private String password;

    private String realName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName == null ? null : realName.trim();
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", username='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", realname='" + realName + '\'' +
                '}';
    }
}
   //    
    @RequestMapping("/addproduct")
    public Object addproduct(@Valid ProductDto model, BindingResult result) {
        int errorCount = result.getErrorCount();
        MessagePack messagePack = new MessagePack();
        //           
        if (result.hasErrors()) {
            throw new RuntimeException(result.getFieldError().getDefaultMessage());
        } else {
            Product product = new Product();
            BeanUtils.copyProperties(model, product);
            //     
            int i = Convert.toInt(productService.addProduct(product));
            //         
            if (i > 0) {
                messagePack.setCode(0);
                messagePack.setMessage("      ");
                messagePack.setObject(null);
                messagePack.setStatus("OK");
            } else {
                messagePack.setCode(-1);
                messagePack.setMessage("      ");
                messagePack.setObject(null);
                messagePack.setStatus("error");
            }
        }
        return messagePack;
    }

 
다음으로 전송:https://www.cnblogs.com/smartsmile/p/11625860.html

좋은 웹페이지 즐겨찾기