我知道如何在Spring中用实际值替换占位符,并且在我的应用程序中一切正常,但是我的问题仅针对以下用例。
是否甚至有可能将$ {malformed.email.address}替换为属性文件中的实际值?
@RequestMapping(value = "/forgot-password", method = RequestMethod.POST)
public ResponseEntity<String> forgotPassword(@RequestBody @Email(message = "${malformed.email.address}") @NotNull String emailAddress, UriComponentsBuilder ucBuilder) {
System.out.println("Forgot password for User : " + emailAddress);
try {
userService.forgotPassword(emailAddress);
return new ResponseEntity<String>("If " + emailAddress + " existed then you should receive an email soon.", HttpStatus.OK);
} catch (ServiceException e) {
throw new ControllerRuntimeException(e);
}
}
我已经配置好了:
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
并且我在类路径中有messages.properties文件,并且填充了spring的Environment对象。
这是我的异常处理程序:
@ControllerAdvice
public class ConstraintViolationExceptionHandler {
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleValidationException(ConstraintViolationException e) {
for (ConstraintViolation<?> s : e.getConstraintViolations()) {
return s.getInvalidValue() + ": " + s.getMessage();
}
return "Invalid request supplied";
}
}
这是我得到的答复:
WARN | 2019-02-05 00:21:16,622 |
org.hibernate.validator.internal.engine.messageinterpolation.ElTermResolver | HV000129: EL expression '${malformed.email.address}' references an unknown
property
javax.el.PropertyNotFoundException: ELResolver cannot handle a null base
Object with identifier [malformed]
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:120)
at org.apache.el.parser.AstValue.getValue(AstValue.java:137)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.hibernate.validator.internal.engine.messageinterpolation.ElTermResolver.interpolate(ElTermResolver.java:65)
at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.interpolate(InterpolationTerm.java:64)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:75)
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateExpression(AbstractMessageInterpolator.java:387)
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateMessage(AbstractMessageInterpolator.java:344)