1项目准备
1.1新建项目
在左侧项目列表空白处,右键>New>Project>Web> Dynamic Web project
新建Dynamic Web Project
1.2导入jar包
(jar包下载地址:链接:https://pan.baidu.com/s/1hrV4qKO 密码:bxaa)
解压后,apache-cxf-2.5.9+\apache-cxf-2.5.9\lib下的所有jar包粘贴到项目的lib目录下
1.3生成配置文件web.xml
右键Deployment Descriptor: WebServices>Generate Deployment Descriptor Sutb
1.4新建cxf的配置文件,并配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引入cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 发布 -->
<jaxws:server id="SolrServer" serviceClass="com.KT.Service.searchService" address="/Solr">
<jaxws:serviceBean>
<bean class="com.KT.Service.Impl.searchServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
1.5配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>WebServices</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>KTServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>KTServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
1.6新建接口类
1.7新建实现类
2发布项目
按照配置文件的格式访问WebService
http://localhost:8080/WebServices/Solr?wsdl
3发布过程中遇到的问题:
3.1错误代码
严重: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxf-extension-soap.xml]
Offending resource: ServletContext resource [/WEB-INF/cxf-servlet.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxf-extension-soap.xml]
Offending resource: ServletContext resource [/WEB-INF/cxf-servlet.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf-extension-soap.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
3.2原因及解决办法
可以看出是因为cxf配置文件cxf-servlet.xml里的import出了问题,文件不存在的错误。
import的路径为"classpath:META-INF/cxf/***",项目中路径为Libraries/Web App Libraries/cxf-2.5.9.jar/META-INF/cxf/***,下载的文件有问题,没有cxf*.jar文件所以导致找不到相关的文件,重新下载并导入有cxf.*.jar的cxf的jar包