Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在apache camel中返回异常时的自定义响应_Java_Apache_Apache Camel_Jbossfuse - Fatal编程技术网

Java 如何在apache camel中返回异常时的自定义响应

Java 如何在apache camel中返回异常时的自定义响应,java,apache,apache-camel,jbossfuse,Java,Apache,Apache Camel,Jbossfuse,我正在使用ApacheCamel和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.apach

我正在使用ApacheCamel和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>

但是,当我发送错误的JSON时,它会返回错误的请求,我需要一些自定义的拦截器,这样我就可以用自定义的正文和标题控制返回的响应

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

}  
@Provider
公共类ExceptionHandler实现ExceptionMapper{
@凌驾
公众响应(可丢弃的例外){
System.out.println(“异常类型:”+Exception.getClass().getCanonicalName());
异常。printStackTrace();
if(BadRequestException的异常实例){
返回Response.status(Response.status.BAD_请求)
.header(“意外请求数据”、“BadRequestExceptionont”).build();
} 
返回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>

如果没有清除我没有编译过的文件,请随时进一步询问,我只是从我以前的项目中复制了它
<?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>