使用java和spark处理API路由异常

使用java和spark处理API路由异常,java,spark-framework,Java,Spark Framework,我有一个API,包含POST、补丁、GET和删除的路由。为了捕获各种可能的错误,我必须在每一个模块中创建try-catch块。我当前调用了一个错误类 StandardError-StandardError=新的StandardError(“415”) 问题是在每种路由方法中都有大量的复制 有关spark的部分描述了捕获异常的以下路径。问题是,这只是捕获一个特定的异常,我需要捕获许多异常 我是否需要为每个异常创建一个新的异常路由,或者我是否可以使用get(“/throweexception)”,(

我有一个API,包含POST、补丁、GET和删除的路由。为了捕获各种可能的错误,我必须在每一个模块中创建try-catch块。我当前调用了一个错误类

StandardError-StandardError=新的StandardError(“415”)

问题是在每种路由方法中都有大量的复制

有关spark的部分描述了捕获异常的以下路径。问题是,这只是捕获一个特定的异常,我需要捕获许多异常

我是否需要为每个异常创建一个新的异常路由,或者我是否可以使用
get(“/throweexception)”,(请求、响应)->{
处理所有错误

get("/throwexception", (request, response) -> {
    throw new YourCustomException();
});

exception(YourCustomException.class, (exception, request, response) -> {
    // Handle the exception here
});
get("/throwexception", (request, response) -> {
    throw new YourCustomException();
});

exception(YourCustomException.class, (exception, request, response) -> {
    // Handle the exception here
});