Spring 引导中的 DTO 验证不工作
DTO validation in Spring Boot not working
我有一个带有 POST 方法的控制器:
@RestController
@RequestMapping(value = "/creditDetails", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
@Validated
public class CreditDetailsController {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public CreditDetailsResponse createCreditDetails(@RequestBody @Valid CreditDetailsRequestWithoutId request) {
return CreditDetailsResponse.convertToResponse(creditDetailsService.createCreditDetails(request));
}
}
和 DTO:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreditDetailsRequestWithoutId {
@DecimalMax("10_000_000")
private BigDecimal creditLimit;
@DecimalMin("0")
@DecimalMax("20")
private BigDecimal creditPercent;
private UUID bankId;
}
当我以 111% 的比例传递 CreditDetailsWithoutId 实例时,我没有收到任何错误。为什么我的验证不起作用?
如果重要我使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.3</version>
</dependency>
尝试将十进制值传递给@DecimalMin, @DecimalMax,:
@DecimalMax:字段的值或属性必须是小于或等于value元素中数字的十进制值。 reference
@DecimalMin("0.0")
@DecimalMax("20.0")
private BigDecimal creditPercent;
稍后编辑:删除@Data 并添加基本的 getter 和 setter 解决了我这边的问题,希望它有效。
// @Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreditDetailsRequestWithoutId {
@DecimalMax("1E+7")
private BigDecimal creditLimit;
@DecimalMin("0.0")
@DecimalMax("20.0")
private BigDecimal creditPercent;
public BigDecimal getCreditPercent(){
return creditPercent;
}
public void setCreditPercent(BigDecimal creditPercent){
this.creditPercent = creditPercent;
}
public BigDecimal getCreditLimit(){
return creditLimit;
}
public void setCreditLimit(BigDecimal creditLimit){
this.creditLimit = creditLimit;
}
}
您必须在控制器中添加 ExceptionHandler class:
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
我刚打电话给 maven clean install
并开始验证
我有一个带有 POST 方法的控制器:
@RestController
@RequestMapping(value = "/creditDetails", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
@Validated
public class CreditDetailsController {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public CreditDetailsResponse createCreditDetails(@RequestBody @Valid CreditDetailsRequestWithoutId request) {
return CreditDetailsResponse.convertToResponse(creditDetailsService.createCreditDetails(request));
}
}
和 DTO:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreditDetailsRequestWithoutId {
@DecimalMax("10_000_000")
private BigDecimal creditLimit;
@DecimalMin("0")
@DecimalMax("20")
private BigDecimal creditPercent;
private UUID bankId;
}
当我以 111% 的比例传递 CreditDetailsWithoutId 实例时,我没有收到任何错误。为什么我的验证不起作用? 如果重要我使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.3</version>
</dependency>
尝试将十进制值传递给@DecimalMin, @DecimalMax,:
@DecimalMax:字段的值或属性必须是小于或等于value元素中数字的十进制值。 reference
@DecimalMin("0.0")
@DecimalMax("20.0")
private BigDecimal creditPercent;
稍后编辑:删除@Data 并添加基本的 getter 和 setter 解决了我这边的问题,希望它有效。
// @Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreditDetailsRequestWithoutId {
@DecimalMax("1E+7")
private BigDecimal creditLimit;
@DecimalMin("0.0")
@DecimalMax("20.0")
private BigDecimal creditPercent;
public BigDecimal getCreditPercent(){
return creditPercent;
}
public void setCreditPercent(BigDecimal creditPercent){
this.creditPercent = creditPercent;
}
public BigDecimal getCreditLimit(){
return creditLimit;
}
public void setCreditLimit(BigDecimal creditLimit){
this.creditLimit = creditLimit;
}
}
您必须在控制器中添加 ExceptionHandler class:
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
我刚打电话给 maven clean install
并开始验证