问题描述
我想扩展 RepositoryRestExceptionHandler 来处理我的 RestController 中的错误.
I want to extend RepositoryRestExceptionHandler to handle errors in my RestController.
我有验证器并且在使用 RestRepository 时工作得很好但是当我使用 RestController 时他们不是所以我做了如下手动验证
I have Validators and working great when using RestRepository but when i use RestController they are not so i did manual Validation like below
Errors errors = new BeanPropertyBindingResult(NumberRange, "range");
NumberRangeValidator.validate(NumberRange, errors);
if(errors.hasErrors()) {
throw new RepositoryConstraintViolationException(errors);
}
这导致错误 500,我想要的结果与 400 错误请求相同,错误在正文中列出.
this is resulting in error 500 , what i want to have as result is same as 400 bad request with errors listed in the body .
我认为我应该扩展下面的类来处理来自我的休息控制器的异常
i think i should have to extend the below class to handle exception from my rest controller
@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class RepositoryRestExceptionHandler { .... }
我试图避免创建与 RepositoryRestExceptionHandler 完全相同的 ControllerAdvice
I am trying to avoid creating ControllerAdvice exactly the same as RepositoryRestExceptionHandler
谢谢
推荐答案
RepositoryRestExceptionHandler 仅限于它自己包中的控制器——这基本上意味着它只在调用 spring 数据休息控制器 (RepositoryEntityController) 时应用.
The RepositoryRestExceptionHandler is restricted to controllers from its own package - this basically means that it is just applied when the spring data rest controller (RepositoryEntityController) is invoked.
所以我只是尝试扩展 RepositoryRestExceptionHandler 并使用包含它应该应用于的控制器的包来注释您的子类:
So I would just try to extend RepositoryRestExceptionHandler and annotate your subclass with a package that includes the controllers it should apply to :
@ControllerAdvice(basePackages="com.my.package")
这样你就不会改变 spring data rest 的异常处理行为,并确保你只是增强了控制器方法的错误处理.
This way you will not change the exception handling behaviour of spring data rest and make sure you just enhance the error handling of your controller methods.
我刚刚尝试了这个 - 所以我的自定义控制器(用 @RepositoryRestController
注释)包含一个抛出 RepositoryConstraintViolationException
的方法.spring 数据剩余异常处理程序 RepositoryRestExceptionHandler
将此异常转换为 BAD_REQUEST (400).
I just tried this - so my custom controller (annotated with @RepositoryRestController
) contains a method that throws a RepositoryConstraintViolationException
. The spring data rest exception handler RepositoryRestExceptionHandler
converts this exception into a BAD_REQUEST (400).
但这不适用于我的自定义控制器方法 - 我收到 INTERNAL_SERVER_ERROR (500).
But this is not applied in my custom controller method - I get a INTERNAL_SERVER_ERROR (500).
因此,我扩展了 RepositoryRestExceptionHandler
并注释了我的控制器建议,使其仅适用于我的控制器的包:
So I extend the RepositoryRestExceptionHandler
and annotate my controller advice so it only applies to the packages of my controller:
@ControllerAdvice(basePackageClasses = DefaultExceptionHandler.class)
public class DefaultExceptionHandler extends RepositoryRestExceptionHandler {
@Autowired
public DefaultExceptionHandler(MessageSource messageSource) {
super(messageSource);
}
}
有了这个 ControllerAdvice,我收到了一个 BAD_REQUEST 400 - 所以它按预期工作.
With this ControllerAdvice in place I receive a BAD_REQUEST 400 - so this works as expected.
这篇关于扩展 RepositoryRestExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!