有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在apache camel中返回异常定制响应

我使用的是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时,它会返回错误的请求,我需要一些自定义的拦截器,这样我就可以用自定义的正文和标题控制返回的响应


共 (1) 个答案

  1. # 1 楼答案

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

    @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>