如何在Spring Boot中使用@RequestParam自定义验证



我在Spring Boot示例中检测自定义验证时有一个问题。我定义了一些验证,包括颜色验证器,文本长度验证器和图像大小验证器。

当我尝试发送带有非验证值的请求时,它成功通过而不是抛出错误。

我该如何修复它?我还分享了我的repo如下所示

下面是控制器

@Validated
public class QrController {

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> createQrCode(@RequestParam String text,
@RequestParam String size,
@RequestParam String color,
@RequestParam String backgroundColor,
@RequestParam(required = false) MultipartFile imageFile) throws IOException {

CreateQrRequest request = CreateQrRequest.builder()
.text(text)
.backgroundColor(backgroundColor)
.color(color)
.size(size)
.build();

....

}
}

下面是CreateQrRequest

@Data
@Builder
public class CreateQrRequest {
@NotBlank
@TextLength
private String text;
@NotBlank
@ImageSize
private String size;
@NotBlank
@Color
private String color;
@NotBlank
@Color
private String backgroundColor;
}

下面是Color注释

@Documented
@Constraint(validatedBy = ColorValidator.class)
@Target(value = {ElementType.FIELD , ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Color {
String message() default "Invalid color hex code";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

下面是ColorValidator

public class ColorValidator implements ConstraintValidator<Color, String> {
@Override
public void initialize(Color constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(String color, ConstraintValidatorContext constraintValidatorContext) {
return ValidatorUtil.validColor(color);
}
}

我还将ElementType.PARAMETER添加到每个验证注释中,如@Color,@TextLength@ImageSize

接下来我修改了如下所示的控制器方法

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> createQrCode(@RequestParam @TextLength String text,
@RequestParam @ImageSize String size,
@RequestParam @Color String color,
@RequestParam @Color String backgroundColor,
@RequestParam(required = false) MultipartFile imageFile) throws IOException {

我得到了如下所示的问题

jakarta.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'com.example.qrgeneratorexample.utils.annotation.TextLength' validating type 'java.lang.String'. Check configuration for 'createQrCode.text'

这是repo: Link

我尝试使用下面所示的所有这些步骤来修复它

1)给控制器方法添加约束注释

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> createQrCode(@RequestParam @TextLengthConstraint @NotBlank String text,
@RequestParam @ImageSizeConstraint @NotBlank String size,
@RequestParam @ColorConstraint @NotBlank String color,
@RequestParam @ColorConstraint @NotBlank String backgroundColor,
@RequestParam(required = false) MultipartFile imageFile) throws IOException {

2)定义如下所示的约束

@Documented
@Constraint(validatedBy = ColorValidator.class)
@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ColorConstraint {
String message() default "Invalid color hex code";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

3)添加@Component到验证器

@Slf4j
@Component
public class ColorValidator implements ConstraintValidator<ColorConstraint, String> {
@Override
public void initialize(ColorConstraint constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(String color, ConstraintValidatorContext constraintValidatorContext) {
log.info("ColorValidator | isValid is working");
return ValidatorUtil.validColor(color);
}
}

相关内容

  • 没有找到相关文章

最新更新