Spring启动时Rest资源的NotImplementedException

时间:2018-04-11 11:09:33

标签: rest spring-boot exception-handling

我正在开发Rest Spring启动应用程序,我的代码为:

  1. @SpringBootApplication public class Initializer扩展了SpringBootServletInitializer {

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

    }

  2. 具有CRUD方法的接口

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

    我有一些资源跟我说abcd。 截至目前,我仅实施了ab,因此,如果客户端尝试访问NotImplementedExceptionc,我想抛出自定义d

    我想知道我应该在哪里抛出此异常,是否必须使用ResourceHandlers之类的内容,如果是,如何使用它以及需要哪些配置?

1 个答案:

答案 0 :(得分:0)

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

2-覆盖处理异常的原始方法。对于您的情况:

public handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)

3-在您用来覆盖处理程序的方法中做您想做的事情。

仅此而已。