spring Sping Boot 中的DTO验证不起作用

qgzx9mmu  于 2024-01-05  发布在  Spring
关注(0)|答案(4)|浏览(205)

我有一个POST方法的控制器:

  1. @RestController
  2. @RequestMapping(value = "/creditDetails", produces = MediaType.APPLICATION_JSON_VALUE)
  3. @RequiredArgsConstructor
  4. @Validated
  5. public class CreditDetailsController {
  6. @ResponseStatus(HttpStatus.CREATED)
  7. @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
  8. public CreditDetailsResponse createCreditDetails(@RequestBody @Valid CreditDetailsRequestWithoutId request) {
  9. return CreditDetailsResponse.convertToResponse(creditDetailsService.createCreditDetails(request));
  10. }
  11. }

字符串
和DTO:

  1. @Data
  2. @JsonIgnoreProperties(ignoreUnknown = true)
  3. public class CreditDetailsRequestWithoutId {
  4. @DecimalMax("10_000_000")
  5. private BigDecimal creditLimit;
  6. @DecimalMin("0")
  7. @DecimalMax("20")
  8. private BigDecimal creditPercent;
  9. private UUID bankId;
  10. }


当我传递CreditDetailsWithoutId示例时,我没有得到任何错误。为什么我的验证不起作用?如果有关系,我使用

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-validation</artifactId>
  4. <version>2.6.3</version>
  5. </dependency>


的数据

bweufnob

bweufnob1#

尝试将十进制值传递给@DecimalMin,@DecimalMax,:

  • @DecimalMax:字段或属性的值必须是小于或等于value元素中的数字的十进制值。* reference
  1. @DecimalMin("0.0")
  2. @DecimalMax("20.0")
  3. private BigDecimal creditPercent;

字符串

**后来编辑:**删除@Data并添加基本的getter和setter修复了我这边的问题,希望它能工作。

  1. // @Data
  2. @JsonIgnoreProperties(ignoreUnknown = true)
  3. public class CreditDetailsRequestWithoutId {
  4. @DecimalMax("1E+7")
  5. private BigDecimal creditLimit;
  6. @DecimalMin("0.0")
  7. @DecimalMax("20.0")
  8. private BigDecimal creditPercent;
  9. public BigDecimal getCreditPercent(){
  10. return creditPercent;
  11. }
  12. public void setCreditPercent(BigDecimal creditPercent){
  13. this.creditPercent = creditPercent;
  14. }
  15. public BigDecimal getCreditLimit(){
  16. return creditLimit;
  17. }
  18. public void setCreditLimit(BigDecimal creditLimit){
  19. this.creditLimit = creditLimit;
  20. }
  21. }

展开查看全部
mbjcgjjk

mbjcgjjk2#

我刚刚调用了maven clean install并开始验证

yh2wf1be

yh2wf1be3#

我的工作是:
pom.xml:

  1. <dependency>
  2. <groupId>org.hibernate.validator</groupId>
  3. <artifactId>hibernate-validator</artifactId>
  4. <version>8.0.1.Final</version>
  5. </dependency>

字符串
控制器:

  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/api/v1/auth")
  4. public class AuthHubController {
  5. @PostMapping("/authenticate")
  6. public ResponseEntity<?> authenticate(HttpServletRequest request, @Validated @RequestBody AuthRequestDto authRequestDto) {
  7. }


Dto:

  1. public class AuthRequestDto {
  2. @Pattern(regexp = "\\d{16}", message = "Card number must contain exactly 16 digits")
  3. String cardNumber; //card number
  4. }


完全@验证

展开查看全部
ffx8fchx

ffx8fchx4#

你必须在你的控制器类中添加EexceptionError:

  1. import org.springframework.web.bind.MethodArgumentNotValidException;
  2. import org.springframework.web.bind.annotation.ExceptionHandler;
  3. @ResponseStatus(HttpStatus.BAD_REQUEST)
  4. @ExceptionHandler(MethodArgumentNotValidException.class)
  5. public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
  6. Map<String, String> errors = new HashMap<>();
  7. ex.getBindingResult().getAllErrors().forEach((error) -> {
  8. String fieldName = ((FieldError) error).getField();
  9. String errorMessage = error.getDefaultMessage();
  10. errors.put(fieldName, errorMessage);
  11. });
  12. return errors;
  13. }

字符串

展开查看全部

相关问题