我正在尝试在Spring Boot REST控制器中添加一些自定义bean验证,使用ResponseEntityExceptionHandler
注释扩展@ControllerAdvice
类并覆盖#handleMethodArgumentNotValid(MethodArgumentNotValidException e, HttpHeaders headers, HttpStatus status, WebRequest request)
方法。在这种方法中,我试图通过FieldError
将给定的messageSource
转换为本地化消息。虽然在尝试使用通过Hibernate验证程序可能实现的消息参数时收到NumberFormatException
,但还是这样。
我正在使用以下依赖项:
org.hibernate.validator:hibernate-validator
(最终版本:6.0.11)org.springframework:spring-web
(5.0.8.RELEASE)org.springframework:spring-webmvc
(5.0.8.RELEASE)所有内容都通过org.springframework.boot:spring-boot-starter-web
(2.0.4.RELEASE)提供。
考虑使用以下REST控制器:
@RestController
public class FooController {
@PostMapping(value = "/foo")
public void submitFooRequest(@Validated @RequestBody FooRequest fooRequest) {
// ....
}
}
FooRequest bean具有一个自定义bean验证批注和约束验证器:
bean FooRequest:
@Getter
@Setter
@ValidBarRequest
public class FooRequest {
private String fieldFoo;
private BarRequest barRequest;
}
Bean BarRequest:
@Getter
@Setter
public class BarRequest {
private String fieldBar;
}
验证批注:
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = BarRequestValidator.class)
@Documented
public @interface ValidBarRequest {
String message() default "{org.example.validation.constraints.ValidBarRequest.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String fieldFoo() default "fieldFoo";
String barRequestFieldBar() default "barRequest.fieldBar";
}
验证约束验证器:
@Log4j2
public class BarRequestValidator implements ConstraintValidator<ValidBarRequest, Object> {
// ....
@Override
public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
if (/* some condition */) {
HibernateConstraintValidatorContext hibernateValidatorContext = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class);
hibernateValidatorContext.disableDefaultConstraintViolation();
hibernateValidatorContext.addMessageParameter("fieldFoo", "some value...").buildConstraintViolationWithTemplate("{org.example.validation.constraints.ValidBarRequest.message}")
.addPropertyNode("barRequest.fieldBar").addConstraintViolation();
return false;
}
return true;
}
}
但是,通过带@ControllerAdvice注释的Bean并使用Spring messageSource,以下消息(在messages.properties中)引发了NumberFormatException:
ValidBarRequest.fooRequest.barRequest.fieldBar=must be lower or equal than {fieldFoo}
@ControllerAdvice bean:
@ControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {
@Autowired private MessageSource messageSource;
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException e, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<ErrorDetails> errorDetails = new ArrayList<>();
for (FieldError fieldError : e.getBindingResult().getFieldErrors()) {
errorDetails.add(new ErrorDetails(fieldError.getField(), messageSource.getMessage(fieldError, Locale.getDefault())));
}
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
@Getter
@AllArgsConstructor
class ErrorDetails {
private String field;
private String message;
}
}
这会导致以下异常:Caused by: java.lang.NumberFormatException: For input string: "fieldFoo"
我在做什么错?另外,我在@SpringBootApplication
中加入了以下bean:
@Bean
public LocalValidatorFactoryBean validator(MessageSource messageSource) {
LocalValidatorFactoryBean localValidatorFactory = new LocalValidatorFactoryBean();
localValidatorFactory.setValidationMessageSource(messageSource);
return localValidatorFactory;
}
答案 0 :(得分:0)
在消息源MessageFormat.format中填充变量,因此必须在大括号中使用数字。
ValidBarRequest.fooRequest.barRequest.fieldBar=must be lower or equal than {0}