如何在Apache Camel Rest API中进行自定义错误处理? [英] How to do custom error handling in an apache camel rest api?

查看:326
本文介绍了如何在Apache Camel Rest API中进行自定义错误处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个apache camel rest api,可以从S3下载文件。我发送json输入(键,存储桶名称,accessKey,secretKey,区域)以编写URI。代码如下所示:

 公共静态类HelloRoute扩展RouteBuilder {

@Override
public void configure (){
rest( /)
.post( file-from-s3)
.route()
.setHeader(AW​​S2S3Constants.KEY,键)
.to( aws2-s3:// bucketname?accessKey = INSERT& secretKey = INSERT& region = INSERT& operation = getObject)
.endRest();
}
}

我能够在通过使用 JSONObject 作为主体来放置上面代码中提到的位置。


如果假设,我在json输入中输入了错误的值(对于前accessKey),出现类似

 的错误,您提供的AWS Access Key ID在我们的记录中不存在。 (服务:S3,状态代码:403,请求ID:6923BEC55C1FD5F1,扩展请求ID:re5Rb7I76j9jvGqhSQXggUUMwOZqsprg22bOGD + BDbuj0zrRrf0FceLaapvpR4KcNDY6GntNiF 错误的访问密钥 

解决方案

您需要使用 onException子句,然后在其中的处理器中创建自己的响应。请参阅: https://camel.apache.org/manual/latest/exception- Clause.html

  onException(TheThrownException.class)
.handled(... )//->取决于您的需要true还是false
.continued(...)//->取决于您的需要,是对还是错
.process( processor1)
.to( direct:error-handling-endpoint)


I have an apache camel rest api which downloads a file from S3. I send json input(key, bucketname, accessKey, secretKey, region) in order to write the URI. The code looks like this:

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();
          }
}

I am able to get the respective json input in the places mentioned in the above code by using a JSONObject as the body.

If suppose, I enter a wrong value in my json input(for ex accessKey), I get an error like

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=)

which is not very user friendly. How can I write a custom error handler for this which will just return an error message for the above case like Wrong Access Key

解决方案

You need to use "onException" clause and create your own response in a processor there. See: 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")

这篇关于如何在Apache Camel Rest API中进行自定义错误处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆