2017-06-06 237 views
2

異常返回定製的響應我使用Apache的駱駝和JBoss導火索,我已經創造了如何在Apache的駱駝

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

可以定義S自定義異常處理器

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

隨意進一步詢問是否犯規清除它,我還沒有編制,只是從我的舊項目複製它 –

+0

謝謝我的理解 –