java - 如何在 apache camel 中返​​回对异常的自定义响应

标签 java apache apache-camel jbossfuse

我正在使用 Apache camel 和 jboss fuse,我创建了下面列出的示例路由蓝图

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:camel="http://camel.apache.org/schema/blueprint"
    xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    <cxf:rsServer address="/testservice" id="testserver" serviceClass="com.company.HelloBean">
    <camelContext id="testContext" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
        <route id="testRoute" >
            <from id="_from1" uri="cxfrs:bean:testserver"/>
                <bean beanType="com.company.HelloBean"
                    id="_bean1" method="hello"/>
        </route>
    </camelContext>
</blueprint>

和实现它的java类

@Path("/testservicenew")
public class HelloBean {


    @POST
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String hello(Person name ) {

        return "Hello:"+name.getName();
    }       
}

但是当我发送错误的 JSON 时,它会返回错误的请求,我使用了一些自定义拦截器,因此我可以使用自定义的正文和 header 来控制返回的响应

最佳答案

您可以将自定义异常处理程序定义为

@Provider
public class ExceptionHandler implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable exception) {

        System.out.println("Exception type:" + exception.getClass().getCanonicalName());

        exception.printStackTrace();
         if (exception instanceof BadRequestException) {

            return Response.status(Response.Status.BAD_REQUEST)
                    .header("unexpected request data", "BadRequestExceptiont").build();

        } 


        return Response.status(Response.Status.REQUEST_TIMEOUT).header("Problemo", "yes problemo").build();
    }

}  

你可以在你的路由中定义它来使用这个类

<?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
        xmlns:camel="http://camel.apache.org/schema/blueprint"
        xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
        <cxf:rsServer address="/testservice" id="testserver" serviceClass="com.company.HelloBean">
  <cxf:providers>
            <bean class="com.company.ExceptionHandler" id="securityException"/>
        </cxf:providers>
    </cxf:rsServer>
        <camelContext id="testContext" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
            <route id="testRoute" >
                <from id="_from1" uri="cxfrs:bean:testserver"/>
                    <bean beanType="com.company.HelloBean"
                        id="_bean1" method="hello"/>
            </route>
        </camelContext>
    </blueprint>

关于java - 如何在 apache camel 中返​​回对异常的自定义响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379693/

相关文章:

php - Apache 在测试服务器上设置多个项目

java - 如何从 JFrame 2 检索 JFrame 1 中设置的类字段

java - 如何获取队列中的第 n 个项目?

PHP - 非破坏性输入清理

jquery - XHR 不起作用,因为 "Origin is not allowed by Access-Control-Allow-Origin"

apache-camel - 如何使用 Java DSL 在 Apache Camel 中调用带参数的方法

java - 删除对象节点树中具有空值的 JSON 对象

java - 如何在固定时间间隔后运行 Camel 计时器,但仅在给定的时间范围内

java - 考虑集合属性的表单的脏状态

java - Java:检测变量是字符串还是整数