spring 引导中 Rest 资源的 NotImplementedException

NotImplementedException for Rest resources in spring boot

我正在开发一个 Rest Spring 引导应用程序,我的代码为:

  1. @SpringBootApplication public class 初始化器扩展 SpringBootServletInitializer {

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

    }

  2. 具有CRUD方法的接口

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

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

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

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

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

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

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

就这些了。