2011-10-19 39 views
2

我正在使用Apache CXF和Spring開發Web服務。我的界面和配置已配置好,以便我擁有REST和SOAP服務。 Apache CXF沒有把XML文檔啓動:<?xml version="1.0" encoding="UTF-8"?>Apache CXF JAX-RS服務缺少XML文檔啓動

對於SOAP服務,我實現了一個攔截器,它的工作原理完美。下面的代碼:

public class CustomHeaderInterceptor extends AbstractPhaseInterceptor<Message> { 

public CustomHeaderInterceptor() { 
    super(Phase.PRE_STREAM); 
    addBefore(StaxOutInterceptor.class.getName()); 
} 

@Override 
public void handleMessage(Message message) throws Fault { 
    message.put(Message.ENCODING, "UTF-8"); 
    message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, Boolean.TRUE); 
} 
} 

攔截器與以下配置增加:

<bean id="customHeader" class="com.minervanetworks.xtv.stb.utils.CustomHeaderInterceptor" /> 

<cxf:bus> 
    <cxf:inInterceptors> 
     <ref bean="logInbound" /> 
    </cxf:inInterceptors> 
    <cxf:outInterceptors> 
     <ref bean="customHeader" /> 
     <ref bean="logOutbound" /> 
    </cxf:outInterceptors> 
    <cxf:inFaultInterceptors> 
     <ref bean="logInbound" /> 
    </cxf:inFaultInterceptors> 
    <cxf:outFaultInterceptors> 
     <ref bean="logOutbound" /> 
    </cxf:outFaultInterceptors> 
</cxf:bus> 

不幸的是這不適合我的JAX-RS服務器的工作。 「StaxOutInterceptor.FORCE_START_DOCUMENT」由StaxOutInterceptor處理,當我使用JAX-RS時它不在鏈中。它不能手動添加,因爲它依賴於處於*結束階段的StaxOutEndingInterceptor,並在JAXRSOutInterceptor之後調用。

我也嘗試實現處理程序爲同一目的,但沒有任何成功。

這裏是我的JAXRS服務器配置:

<jaxrs:server id="restServer" address="/rest"> 
    <jaxrs:providers> 
     <ref bean="systemExceptionMapper" /> 
     <ref bean="jaxbProvider" /> 
    </jaxrs:providers> 
    <jaxrs:serviceBeans> 
     ... 
    </jaxrs:serviceBeans> 
    <jaxrs:extensionMappings> 
     <entry key="json" value="application/json" /> 
     <entry key="xml" value="application/xml" /> 
     <entry key="feed" value="application/atom+xml" /> 
     <entry key="html" value="text/html" /> 
    </jaxrs:extensionMappings> 
</jaxrs:server> 

任何形式的幫助 - 意見,建議,無論將appriciated!

回答

3

經過大量的時間投入這個問題,我發現了以下解決方案。

當編輯集合時,Apache CXF不會將XML文檔啓動,這是我的情況。 JAXBElement提供者有點遺漏。我意識到編組單個對象時一切都很好。

所以對我來說顯而易見的解決辦法是換我收藏的集合對象,像這樣:

公共類CollectionWrapper {

私人列表集合;

...

}

重構我的方法返回CollectionWrapper,而不是名單做了工作。

+1

+1與社區分享您的解決方案。你還應該將自己的問題標記爲答案。 – sra