Java ControllerAdvice没有';无法捕获绑定结果错误

Java ControllerAdvice没有';无法捕获绑定结果错误,java,spring-boot,unit-testing,controller-advice,Java,Spring Boot,Unit Testing,Controller Advice,我正在尝试覆盖BindingResult中的默认错误消息。我像这样执行控制器建议 @ControllerAdvice 公共最终类DefaultControllerAdvice{ 私有静态最终字符串错误\消息\格式化程序=“-”; @ExceptionHandler(BindException.class) @ResponseStatus(值=HttpStatus.BAD_请求) 公共列表handleValidationException(最终BindException){ 返回异常。getBin

我正在尝试覆盖BindingResult中的默认错误消息。我像这样执行控制器建议

@ControllerAdvice
公共最终类DefaultControllerAdvice{
私有静态最终字符串错误\消息\格式化程序=“-”;
@ExceptionHandler(BindException.class)
@ResponseStatus(值=HttpStatus.BAD_请求)
公共列表handleValidationException(最终BindException){
返回异常。getBindingResult()
.getAllErrors()
.stream()
.filter(错误->字段错误的错误实例)
.map(objectError->(FieldError)objectError)
.map(errorMessageFormatter)
.collect(Collectors.toList());
}
私有最终函数errorMessageFormatter=
error->error.getField()+error\u MESSAGE\u FORMATTER+error.getDefaultMessage();
}
还有我的控制器

@PostMapping(value=“/register”)
私有字符串注册后(@modeldattribute@Valid final UserCreateFormDto user,final BindingResult,
最终重定向属性重定向属性,最终WebRequest WebRequest){
试一试{
if(result.hasErrors()){
redirectAttributes.addFlashAttribute(“org.springframework.validation.BindingResult.user”,结果);
redirectAttributes.addFlashAttribute(“用户”,user);
抛出新的BindException(结果);
}
if(userService.checkEmailExist(user.getEmail())){
抛出新的UserNotExistsException(“具有电子邮件的用户:+User.getEmail()+”已存在”);
}
最终注册用户=userService.createNewUserAccount(用户);
最后一个字符串appUrl=webRequest.getContextPath();
eventPublisher.publishEvent(
新注册完成事件(已注册,webRequest.getLocale(),appUrl));
返回“重定向:/login?成功”;
}捕获(UserNotExistsException错误){
返回“重定向:/register?存在”;
}捕获(异常错误){
返回“重定向:/register”;
}
}
和测试用例

@测试
public void shouldnotCreateUserWhenUserNamesEmpty()引发异常{
//给定
最终用户CreateFormdTo用户CreateFormdTo=createUserCreateForm();
最终用户=createUser();
给定(userService.checkEmailExist(userCreateFormDto.getEmail())。将返回(false);
给定(userService.createNewUserAccount(any(UserCreateFormDto.class)).willReturn(user);
//什么时候
最终MVC结果响应=模拟MVC
.perform(post(“/寄存器”)。与(csrf())
.contentType(MediaType.MULTIPART\u表单\u数据)
.param(“用户名”,“参数”)
.param(“电子邮件”,userCreateFormDto.getEmail())
.param(“密码”,Arrays.toString(userCreateFormDto.getPassword()))
.param(“matchingPassword”,Arrays.toString(userCreateFormDto.getMatchingPassword()))
.andReturn();
//然后
断言(response.getFlashMap().isEmpty()).isFalse();
assertThat(response.getResponse().getStatus()).isEqualTo(HttpStatus.FOUND.value());
断言(response.getResponse().getRedirectedUrl()).isEqualTo(“/register”);
验证(userService,times(1)).checkEmailExist(userCreateFormDto.getEmail());
验证(userService,times(0)).createNewUserAccount(any(UserCreateFormDto.class));
我的问题是如何获取bindingResult默认错误消息?
我想测试验证输入字段时收到的错误消息。

您的控制器是否有控制器/RestController注释?默认情况下,ControllerAdvice适用于使用@controller注释的所有类,因此是一个问题。这是控制器注释。我有一个关于RestController和RestControllerA的工作示例哦,好吧,我忘了添加我的测试用例xdOkey,我通过从flashMap获取BeanProperties BindingResult对象解决了这个问题。你的控制器有controller/RestController注释吗?默认情况下,ControllerAdvice应用于所有使用@controller注释的类,因此这个问题是这样的。这是控制器注释。我有一个工作示例我忘了添加我的测试用例xdOkey,我通过从flashMap获取BeanPropertiesBindingResult对象解决了这个问题。