如何在 apache camel rest api 中进行自定义错误处理?

How to do custom error handling in an apache camel rest api?

我有一个 apache camel rest api 从 S3 下载文件。我发送 json input(key, bucketname, accessKey, secretKey, region) 以编写 URI。代码如下所示:

public static class HelloRoute extends RouteBuilder {
       
   @Override
   public void configure() {
     rest("/")
     .post("file-from-s3")
     .route()
     .setHeader(AWS2S3Constants.KEY, key)
     .to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT&region=INSERT&operation=getObject")
      .endRest();
          }
}

我可以通过使用 JSONObject 作为正文在上述代码中提到的地方获得相应的 json 输入。

如果假设,我在我的 json 输入中输入了错误的值(对于 ex accessKey),我会收到类似

的错误
The AWS Access Key Id you provided does not exist in our records. (Service: S3, Status Code: 403, Request ID: 6923BEC55C1FD5F1, Extended Request ID: re5Rb7I76j9jvGqhSQXgjUoMwOZqsprg22bOGD+BDbuj0zrRrf0FceLaapvpR4KcNDY6GntNiF0=)

这对用户来说不是很友好。我如何为此编写一个自定义错误处理程序,它只会 return 上述情况的错误消息,如 Wrong Access Key

您需要使用“onException”子句并在那里的处理器中创建您自己的响应。参见:https://camel.apache.org/manual/latest/exception-clause.html

onException(TheThrownException.class)
    .handled(...) //--> depend on your need true or false
    .continued(...) //--> depend on your need true or false
    .process("processor1")
    .to("direct:error-handling-endpoint")