我正在开发一个 Rest Spring 启动应用程序,我的代码如下:
@SpringBootApplication 公共(public)类 Initializer 扩展了 SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Initializer.class); }
具有CRUD方法的接口(interface)
- 许多实现接口(interface)的类
- Controller 类
- 一个带有
@ControllerAdvice
的异常处理类,其中一个方法带有@ExceptionHandler(NotImplementedException.class){//message }
我有一些资源,比如 a
、b
、c
、d
。
到目前为止,我只实现了 a
和 b
,因此如果客户端尝试访问 ,我想抛出一个自定义的
和 NotImplementedException
cd
。
我想知道我应该在哪里抛出这个异常,我是否必须使用像ResourceHandlers
这样的东西,如果是,如何使用它以及需要什么配置?
最佳答案
1 - Controller 建议类必须 extends ResponseEntityExceptionHandler
2 - 覆盖处理异常的原始方法。在你的情况下:
public ResponseEntity<Object> handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
3 - 在覆盖处理程序的方法中执行您想执行的操作。
就这些。
关于rest - spring boot 中 Rest 资源的 NotImplementedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773551/