2017-05-04 185 views
0

目前我正努力在駱駝CXF-RS端點中設置一個自定義標頭。我想設置一個名稱爲「apps-client」的頭文件。此響應標頭將由其餘端點的使用者使用。如何在CXF-RS Camel中設置自定義響應頭?

我嘗試了添加DefaultCxfRsBinding類。

<camel:route id="cxf-restful-routes"> 
    <camel:from uri="cxfrs:bean:cxf.restful.endpoint?binding=#CXFRestfulBindings" /> 
    <camel:to uri="direct:postJson" /> 
</camel:route> 

是否有任何其他方式來設置響應頭?

感謝您的幫助..!

回答

0

我可以在最終響應中設置自定義標題。我通過在CXFEndpoint中添加outInterceptor來完成。我已添加AbstractPhaseInterceptor。首先,我必須在駱駝路線中設置CamelExchange中的標題。我通過從CamelExchange中讀取它,將其設置爲#CXFRestfulBindings.populateCxfRsResponseFromExchange()中的CXFExchange。現在,這個頭文件將在CXFExchange中可用,並且可以輕鬆獲取。我在我通過擴展AbstractPhaseInterceptor創建的一個攔截器中讀取了此標題。比我在PROTOCOL_HEADERS地圖中添加了這個標題。

以下是代碼片段,

public class OutResponseInterceptor extends AbstractPhaseInterceptor<Message>{ 

    public RestResponseInterceptor(String phase) { 
     super(Phase.MARSHAL); 
    } 

    @Override 
    public void handleMessage(Message message) throws Fault { 
     try { 
      @SuppressWarnings("unchecked") 
      MultivaluedMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS); 

      if (headers == null) { 
       headers = new MetadataMap<String, Object>(); 
      } 

      headers.putSingle("apps-client", (String) message.getExchange().get("apps-client")); 
      message.put(Message.PROTOCOL_HEADERS, headers); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

謝謝你.. !!

0

您是否嘗試將此標頭添加到Exchange?端點將把它添加到HTTP響應中。

<camel:route id="cxf-restful-routes"> 
    <camel:from uri="cxfrs:bean:cxf.restful.endpoint?binding=#CXFRestfulBindings" /> 
    <camel:setHeader headerName="apps-client"> 
     <constant>Your value here</constant> <!-- or use <simple> language --> 
    </camel:setHeader> 
    <camel:to uri="direct:postJson" /> 
</camel:route> 
+0

不,它沒有奏效。根據您的建議,它將設置爲CamelExchange,但不在CXFExchange的協議標頭中。 –

相關問題