springboot validation 검사 파라미터
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.