替代@XmlRootElement?

时间:2012-11-12 11:25:18

标签: java rest cxf

将现有的statefull服务公开为RESTfull服务。

我不想对任何现有的java类进行任何更改。我已经能够使用spring-config.xml

配置其他注释,例如@path,@ GET

弹簧-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 个答案:

答案 0 :(得分:1)

来自jaxrs-data-bindings上的Apache-cxf文档:

  

除了使用@XmlRootElement和Collection包装器之外,还可以   提供一个Object工厂,告诉JAXB如何编组给定的   type(如果是Collections - 其模板类型)。另一种选择是   直接从/在给定方法中返回/接受JAXBElement。

     

另一种选择是注册一个或多个JAX-RS ContextResolver提供程序   能够为许多不同类型创建JAXBContexts。该   默认的JAXBElementProvider会先检查这些解析器   试图自己创建一个JAXBContext。