Java SpringBoot 실체 클래스 데이터 자동 검증
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.