2012-11-12 46 views
2

正在將現有的有狀態服務公開爲REST滿服務。替代@XmlRootElement?

我不想向任何現有的Java class.I任何改動已經能夠通過配置其他註釋,比如@path,@GET spring-config.xml

春天-config.xml中

<!-- Inquiry Services --> 
    <bean id="retrieveContactHistoryBP" class="com.csc.fs.ws.contact.history.impl.RetrieveContactHistoryBPService"/>   
    <!-- Update Services --> 
    <bean id="startContactBP" class="com.csc.fs.ws.contact.impl.StartContactBPService"/> 

    <!-- REST services --> 
    <bean id="startContactBPRest" class="com.csc.fs.rest.contact.StartContactBP" scope="prototype" /> 
    <bean id="retrieveContactHistoryBPRest" class="com.csc.fs.rest.contact.RetrieveContactHistoryBP" scope="prototype" /> 

    <!-- Exposing beans as rest services --> 
    <jaxrs:server id="restServer" address="/rest/"> 
     <jaxrs:model id="restModel"> 
      <jaxrs:resource name="com.csc.fs.rest.contact.RetrieveContactHistoryBP" path="retrieveContactHistoryBP"> 
       <jaxrs:operation name="retrieve" path="{partyId}" consumes="application/json" produces="application/json" verb="GET"> 
        <jaxrs:param name="req" type="CONTEXT"/> 
        <jaxrs:param name="partyId" type="PATH"/> 
       </jaxrs:operation> 
      </jaxrs:resource> 
      <jaxrs:resource name="com.csc.fs.rest.contact.StartContactBP" path="startContactBP"> 
       <jaxrs:operation name="startContact" path="/" consumes="application/json" produces="application/json" verb="PUT"> 
        <jaxrs:param name="req" type="CONTEXT"/> 
        <jaxrs:param name="startContact" type="REQUEST_BODY"/> 
       </jaxrs:operation> 
      </jaxrs:resource> 
     </jaxrs:model> 
     <jaxrs:serviceBeans> 
<!--    <ref bean="startContactBPRest"/> --> <!-- Instead configure above --> 
<!--    <ref bean="retrieveContactHistoryBPRest"/> --> 
     </jaxrs:serviceBeans> 
     <jaxrs:extensionMappings> 
      <entry key="feed" value="application/atom+xml"/> 
      <entry key="json" value="application/json"/> 
      <entry key="xml" value="application/xml"/> 
      <entry key="html" value="text/html"/> 
     </jaxrs:extensionMappings> 

     <jaxrs:providers> 
      <ref bean="jaxbProvider"/> 
      <ref bean="jsonProvider" /> 
     </jaxrs:providers> 
    </jaxrs:server> 

我面臨的問題是與@XmlRootElement。我沒有成功通過xml進行配置。

,並試圖訪問REST服務

org.apache.cxf.interceptor.Fault 
    org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:67) 
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315) 
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113) 
    org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:105) 
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461) 
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:188) 
    org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159) 


root cause 

java.lang.NullPointerException 
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleOperation(WadlGenerator.java:310) 
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleResource(WadlGenerator.java:253) 
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleRequest(WadlGenerator.java:185) 
    org.apache.cxf.jaxrs.impl.RequestPreprocessor.checkMetadataRequest(RequestPreprocessor.java:189) 
    org.apache.cxf.jaxrs.impl.RequestPreprocessor.preprocess(RequestPreprocessor.java:82) 
    org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:112) 
    org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:88) 
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) 
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113) 
    org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:105) 
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461) 
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:188) 
    org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159) 

所以當我收到以下錯誤,是否存在一種配置在XmlRoot註釋外部信息的方式,所以我們沒有將它添加到Java代碼?

回答

1

從Apache的CXF文檔上jaxrs-data-bindings

替代地使用@XmlRootElement和收集包裝,可以提供 對象工廠它會告訴JAXB如何封送給定 類型(在的情況下集合 - 它的模板類型)。另一種選擇是 返回/直接從給定方法中接受JAXBElement。

另一種選擇是註冊一個或多個JAX-RS ContextResolver提供程序 ,該提供程序能夠爲多種不同類型創建JAXBContext。 默認JAXBElementProvider將在 嘗試自行創建JAXBContext之前首先檢查這些解析器。

+0

網址被破壞,現在使用:https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Data+Bindings – mvermand