springboot validation 검사 파라미터

3680 단어 springboot
vaidation 을 사용 하여 전후 단 매개 변수 검증 을 완료 하려 면 먼저 의존 도 를 가 져 옵 니 다. (spring boot 2.0 이상 의존 도 를 가 져 올 필요 가 없습니다)

 javax.validation
 validation-api
 2.0.1.Final


매개 변수 설명:
   
     
  




@AssertFalse
   ,   
      ,       


@AssertFalse
   ,   
      ,       


@DecimalMax( = x) 
BigDecimal,BigInteger,String,byte,short,int,long
            @ DecimalMax     


@DecimalMin( = x) 
BigDecimal,BigInteger,String,byte,short,int,long
            @ DecimalMin     


@Digits(  =    ,  =    )
BigDecimal,BigInteger,String,byte,short,int,long
                    


@    
 CharSequence
             ,                         


@Future(  =    ,  =    )
java.util.Date,java.util.Calendar
        (    )      


@FutureOrPresent(integer =    ,fraction =    )
java.util.Date,java.util.Calendar
        (    )              


@  
java.util.Date,java.util.Calendar
        (    )      


@PastOrPresent
java.util.Date,java.util.Calendar
        (    )           


@Max( = x) 
BigDecimal,BigInteger,byte,short,int,long
            @Max     


@Mix( = x) 
BigDecimal,BigInteger,byte,short,int,long
            @Max     


@NotBlank
 CharSequence
           (    ,          0),   @ NotEmpty,@ NotBlank                     


@    
 CharSequence
               (       0,      0)


@NotNull
    
           


@  
    
          


@Pattern(     =     ,flag =)
 
                   


@Size(min =   ,max =   )
   ,  ,     
               (  )      ,     ,    


사용: 실체 류 에 주 해 를 넣 으 면 됩 니 다. 수 요 를 보고 정 합 니 다. 이 단 계 는 프레젠테이션 을 위해 서 입 니 다.
/** 
* @author     :   
* @version      : 2019 7 8    5:15:26 
* @explain     : 
*/
public class User {
	
	@NotBlank(message = "userName    ")
    @Size(min=2, max=30, message = "userName     2 30  ")
	private String userName;
	@NotBlank(message = "password    ")
    @Size(min=2, max=30, message = "password     2 30  ")
	private String password;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

그리고 Controller 에 @ Validated 주해 검 사 를 추가 합 니 다.
@PostMapping("/test")
    @ApiOperation(value="    ",notes="    ")
	public Map test(@Validated @RequestBody User user){
		Map mp = new ConcurrentHashMap<>(10000);
 		mp.put("user", user);
		return mp;
	}

@ Validated 와 @ RequestBody 작업 시 이상 이 발생 하여 통 일 된 이상 처 리 를 정의 합 니 다.
/** 
* @author     :   
* @version      : 2019 6 6    11:17:06 
* @explain     :     
*/
@RestControllerAdvice
public class ExceptionHandling {
	
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Map exception(MethodArgumentNotValidException e) {
    	Map mp = new ConcurrentHashMap<>(10);
        BindingResult bindingResult = e.getBindingResult();
        List allErrors = bindingResult.getAllErrors();
        List errorMsgs = new ArrayList<>();
        allErrors.forEach(objectError -> {
            FieldError fieldError = (FieldError)objectError;
            errorMsgs.add(fieldError.getDefaultMessage());
        });
        mp.put("code", 400);
		mp.put("message", errorMsgs);//      
        return mp;
    }
    

좋은 웹페이지 즐겨찾기