Java @ControllerAdvice(REST404)-引发错误的异常

Java @ControllerAdvice(REST404)-引发错误的异常,java,spring,spring-mvc,tomcat,spring-boot,Java,Spring,Spring Mvc,Tomcat,Spring Boot,我正在使用SpringMVC3.2.9,目前正在为应用程序进行全局错误处理。我的错误处理类如下所示: @ControllerAdvice public class ErrorController extends ResponseEntityExceptionHandler { @Override protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandli

我正在使用SpringMVC3.2.9,目前正在为应用程序进行全局错误处理。我的错误处理类如下所示:

@ControllerAdvice
public class ErrorController extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //Some code

        return new ResponseEntity(ex, headers, status);
    }

    @Override
    protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //Some code

        return new ResponseEntity(ex, headers, status);
    }
}
/modules/admin/form/addfsdfadf/
我试图通过触发NoSuchRequestHandlingMethodException来引发404错误,希望我的错误处理类能够捕获它并返回ResponseEntity,如前所述。但事实恰恰相反:

如果我向客户提出这样的请求:

@ControllerAdvice
public class ErrorController extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //Some code

        return new ResponseEntity(ex, headers, status);
    }

    @Override
    protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //Some code

        return new ResponseEntity(ex, headers, status);
    }
}
/modules/admin/form/addfsdfadf/
因为这个映射不存在,所以它应该触发所说的异常,对吗?但是会抛出HttpRequestMethodNotSupportedException,并调用handleHttpRequestMethodNotSupported

另一个例子:

/modules/admin/form/1232313423/details/field/addhfddfhfghsdfhs/
这个映射也不存在,但它完全绕过了我的错误处理机制,我在响应中得到了普通的老tomcat404页面(纯html)

我已经对此进行了测试,似乎请求中的字符数以某种方式决定了我将得到哪种响应,在我的情况下,它们都是错误的

有人知道这里发生了什么吗?这是错误还是预期行为?我做错了吗

我真正想要的是处理404,而不获取默认的Tomcat页面或我自己的默认页面。这是REST控制器,如果发生404,我想得到JSON响应……这可能吗?

看看这个答案是否有用: