代码之家  ›  专栏  ›  技术社区  ›  Harsha

spring boot中Rest资源的NotImplementedException

  •  3
  • Harsha  · 技术社区  · 7 年前

    我正在开发Rest Spring引导应用程序,代码如下:

    1. @SpringBootApplication 公共类初始值设定项扩展了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 。 到目前为止,我只实现了 A. B ,因此我想抛出一个自定义 NotImplementedException 如果客户端尝试访问 C D

    我想知道应该在哪里抛出此异常,是否必须使用 ResourceHandlers ,如果是,如何使用,需要什么配置?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shell_Leko    5 年前

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

    2-重写处理异常的原始方法。在您的情况下:

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

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

    仅此而已。