【问题标题】:Spring mockMvc throws error when using ExceptionHandlerSpring mockMvc 在使用 ExceptionHandler 时抛出错误
【发布时间】:2017-12-04 03:13:10
【问题描述】:

我正在使用 SpringMockMvc,在我的控制器中我有 @ExceptionHandler。调用post 请求时,出现以下错误。

Failed to invoke @ExceptionHandler method: public org.springframework.http.ResponseEntity<com.xx.xxx> com.xx.xx.handleException(java.lang.Throwable,org.eclipse.jetty.server.Request)
java.lang.IllegalStateException: Current request is not of type [org.eclipse.jetty.server.Request]: org.springframework.mock.web.MockHttpServletRequest@47fac1bd
        at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.java:97)

如果请求的类型为org.springframework.mock.web.MockHttpServletRequest,我不确定为什么@ExceptionHandler 无法处理

【问题讨论】:

    标签: spring-mvc testing spring-boot spring-test-mvc


    【解决方案1】:

    这永远行不通,因为org.springframework.mock.web.MockHttpServletRequest 不是org.eclipse.jetty.server.Request 的子类型。因此,Spring 不可能为 org.eclipse.jetty.server.Request 类型的参数提供 MockHttpServletRequest 的实例。

    您需要将handleException() 方法中第二个参数的类型更改为javax.servlet.http.HttpServletRequest

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 2019-08-05
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多