尝试使用 ExceptionHandler 捕获 @valid 时出错

发布于 2025-01-10 21:18:50 字数 1478 浏览 0 评论 0原文

我有一个带有这样的 REST 控制器的 Spring Web 应用程序:

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<Resp> do(@Valid @RequestBody(required = true) Body b){

我的对象具有如下验证:

public class Body implements Serializable {
    @NotEmpty
    @Max(value = 32)
    private String nss;

我的 @ExceptionHandler 是这样的:

@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {

      @ExceptionHandler(value = { MethodArgumentNotValidException.class })
      public ResponseEntity<RespError> invalidInput(RuntimeException ex) {
          RespErrorresponse = new RespError();
          return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
      }

我试图捕获无效的异常,但每次我尝试启动应用程序时,我都会'我得到:

java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {public org.springframework.http.ResponseEntity com.algoApp.controller.RestResponseEntityExceptionHandler.invalidInput(java.lang.RuntimeException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}

我应该如何实现处理程序来捕获此异常?

I have a Spring web app with a REST controller like this:

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<Resp> do(@Valid @RequestBody(required = true) Body b){

my object has validations like these:

public class Body implements Serializable {
    @NotEmpty
    @Max(value = 32)
    private String nss;

and my @ExceptionHandler is this:

@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {

      @ExceptionHandler(value = { MethodArgumentNotValidException.class })
      public ResponseEntity<RespError> invalidInput(RuntimeException ex) {
          RespErrorresponse = new RespError();
          return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
      }

I'm trying to catch the not valid exception but everytime I'm trying to start the app, I'm getting:

java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {public org.springframework.http.ResponseEntity com.algoApp.controller.RestResponseEntityExceptionHandler.invalidInput(java.lang.RuntimeException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}

how should I implement the handler to catch this exception instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2025-01-17 21:18:50

根据我的经验,匹配参数是有效的。

@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {

      @ExceptionHandler(value = { MethodArgumentNotValidException.class })
      public ResponseEntity<RespError> invalidInput(MethodArgumentNotValidExceptionex) {
          RespErrorresponse = new RespError();
          return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
      }
}

In my experience, matching arguments works.

@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {

      @ExceptionHandler(value = { MethodArgumentNotValidException.class })
      public ResponseEntity<RespError> invalidInput(MethodArgumentNotValidExceptionex) {
          RespErrorresponse = new RespError();
          return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
      }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文