【问题标题】:NotImplementedException for Rest resources in spring bootSpring Boot 中 Rest 资源的 NotImplementedException
【发布时间】:2018-09-21 05:58:38
【问题描述】:

我正在开发一个 Rest Spring 启动应用程序,我的代码如下:

  1. @SpringBootApplication 公共类 Initializer 扩展 SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Initializer.class);
    }
    

    }

  2. 具有 CRUD 方法的接口

  3. 许多实现接口的类
  4. 控制器类
  5. 带有@ControllerAdvice 的异常处理程序类,其中带有@ExceptionHandler(NotImplementedException.class){ //message } 的方法

我有一些资源,比如a,b,c,d。 到目前为止,我只实现了ab,因此如果客户端尝试访问cd,我想抛出一个自定义的NotImplementedException

我想知道我应该在哪里抛出这个异常,我是否必须使用ResourceHandlers之类的东西,如果是,如何使用它以及需要什么配置?

【问题讨论】:

    标签: rest spring-boot exception-handling


    【解决方案1】:

    1 - 控制器建议类必须extends ResponseEntityExceptionHandler

    2 - 覆盖处理异常的原始方法。在你的情况下:

    public ResponseEntity<Object> handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)

    3 - 在您覆盖处理程序的方法中执行您想要执行的操作。

    就是这样。

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 2016-06-06
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      相关资源
      最近更新 更多