如果命中了仍未实现的剩余方法(例如 get、put),则抛出自定义异常?

throwing a custom exception if a rest method(such as get,put) is hit which is still not implemented?

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

1.

@SpringBootApplication
public class Initializer extends SpringBootServletInitializer {

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

}

2。 实现 RUD

的接口
@Service

public interface IProcess {

     public Object notification(Object input, Object output);

     public Object update(Object input);

     public Object read(Object input);
 }

3.many classes 实现了 IProcess

4.controller classes

5.An 异常处理程序 class 与 @ControllerAdvice

截至目前,我在 Rest 中只实现了 POST 方法,如果 POST(如 get put)以外的方法被客户。 如果我抛出 servlet 异常,它是否满足我的要求,如果是,我如何实现它。

您可以在使用 @ControllerAdvice 的 class 中扩展 ResponseEntityExceptionHandler,它默认处理所有已知的 http 异常。在您的 class 中,您可以覆盖 handleHttpRequestMethodNotSupported 方法和 return 您的自定义消息。无需创建自定义异常。