apache cxf rest service

时间:2015-10-27 21:37:59

标签: java spring tomcat exception cxf

我正在学习如何使用apache cxf构建休息服务,我按照教程进行操作。但是当我运行代码时,我得到了异常

  

严重:上下文初始化失败   org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:   来自类路径资源的XML文档中的第15行   [com / example / rest / cxf.xml]无效;嵌套异常是   org.xml.sax.SAXParseException; lineNumber:15; columnNumber:55;   cvc-elt.1:无法找到元素' bean的声明。在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)     在   org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)     在   org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)     在   org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)     在   org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)     在   org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)     在   org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)     在   org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)     在   org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1574)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1564)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)引起:   org.xml.sax.SAXParseException; lineNumber:15; columnNumber:55;   cvc-elt.1:无法找到元素' bean的声明。在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)     在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)     在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)     在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:880)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)     在   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)     在   com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)     在   com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)     在   org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)     ......还有22个

     

2015年10月28日上午2:52:44 org.apache.catalina.core.StandardContext   listenerStart SEVERE:将上下文初始化事件发送到的异常   监听器类的实例   org.springframework.web.context.ContextLoaderListener   org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:   来自类路径资源的XML文档中的第15行   [com / example / rest / cxf.xml]无效;嵌套异常是   org.xml.sax.SAXParseException; lineNumber:15; columnNumber:55;   cvc-elt.1:无法找到元素' bean的声明。在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)     在   org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)     在   org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)     在   org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)     在   org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)     在   org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)     在   org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)     在   org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)     在   org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1574)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1564)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)引起:   org.xml.sax.SAXParseException; lineNumber:15; columnNumber:55;   cvc-elt.1:无法找到元素' bean的声明。在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)     在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)     在   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)     在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)     在   com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:880)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)     在   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)     在   com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)     在   com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)     在   org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)     ......还有22个

我的cxf.xml文件是这样的

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xsi:schemalocation=
           "http://www.springframework.org/schema/beans      
            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://cxf.apache.org/jaxrs 
            http://cxf.apache.org/schemas/jaxrs.xsd">   

<import resource="classpath:META-INF/cxf/cxf.xml">  
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml">  
<import resource="classpath:META-INF/cxf/cxf-servlet.xml">  
<jaxrs:server address="/" id="connectionService">  

 <jaxrs:servicebeans>             
  <ref bean="order">    </ref></jaxrs:servicebeans> 

    <jaxrs:extensionmappings>    
    <entry key="xml" value="application/xml">   
    </entry></jaxrs:extensionmappings>  
    </jaxrs:server> <bean class="com.example.rest.OrderInfoImpl" id="order">
    </bean>

    </import></import></import></beans>

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

You have declared jaxrs beans inside import class which is wrong, Here is updated beans.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.springframework.org/schema/beans" 
       xsi:schemalocation=
           "http://www.springframework.org/schema/beans      
            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://cxf.apache.org/jaxrs 
            http://cxf.apache.org/schemas/jaxrs.xsd">   

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxrs:server address="/" id="connectionService">  
        <jaxrs:servicebeans>             
            <ref bean="order">    
            </ref>
        </jaxrs:servicebeans> 
        <jaxrs:extensionmappings> 
            <entry key="xml" value="application/xml"></entry>
        </jaxrs:extensionmappings>  
    </jaxrs:server> 

    <bean class="com.example.rest.OrderInfoImpl" id="order" />
</beans>