spring 引导中 Rest 资源的 NotImplementedException
NotImplementedException for Rest resources in spring boot
我正在开发一个 Rest Spring 引导应用程序,我的代码为:
@SpringBootApplication
public class 初始化器扩展 SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Initializer.class);
}
}
具有CRUD方法的接口
- 许多class实现了接口
- 控制器classes
- 异常处理程序 class
@ControllerAdvice
,其中的方法 @ExceptionHandler(NotImplementedException.class){ //message }
我有一些资源说 a
、b
、c
、d
。
到目前为止,我只实现了 a
和 b
,因此如果客户端尝试访问 c
和 d
,我想抛出自定义 NotImplementedException
。
我想知道我应该在哪里抛出这个异常,我是否必须使用类似ResourceHandlers
的东西,如果是,如何使用它以及需要什么配置?
1 - 控制器建议 class 必须 extends ResponseEntityExceptionHandler
2 - 覆盖处理异常的原始方法。在你的情况下:
public ResponseEntity<Object> handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
3 - 在覆盖处理程序的方法中执行您想执行的操作。
就这些了。
我正在开发一个 Rest Spring 引导应用程序,我的代码为:
@SpringBootApplication public class 初始化器扩展 SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Initializer.class); }
}
具有CRUD方法的接口
- 许多class实现了接口
- 控制器classes
- 异常处理程序 class
@ControllerAdvice
,其中的方法@ExceptionHandler(NotImplementedException.class){ //message }
我有一些资源说 a
、b
、c
、d
。
到目前为止,我只实现了 a
和 b
,因此如果客户端尝试访问 c
和 d
,我想抛出自定义 NotImplementedException
。
我想知道我应该在哪里抛出这个异常,我是否必须使用类似ResourceHandlers
的东西,如果是,如何使用它以及需要什么配置?
1 - 控制器建议 class 必须 extends ResponseEntityExceptionHandler
2 - 覆盖处理异常的原始方法。在你的情况下:
public ResponseEntity<Object> handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
3 - 在覆盖处理程序的方法中执行您想执行的操作。
就这些了。