1.Web Service技术,能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件,就可相互交换数据或集成。依据Web Service规范实施的应用之间,无论它们所使用的语言、平台或内部协议是什么,都可以相互交换数据。

 

webservice CXF总结_cxf

 

 

 

2.

SOAP:



即简单对象访问协议 (Simple Object Access Protocol)



它是用于交换 XML (标准通用标记语言下的一个子集)编码信息的轻量级协 议



XML-envelope 为描述信息内容和如何处理内容定义了框架,将程序对象编 码成为 XML 对象的规则,执行远程过程调用 (RPC) 的约定。


XML-envelope—(code)- à XML


SOAP



可以运行在任何其他传输协议上

3.wsdl:

 


就是用机器能阅读的方式提供的一个正式描述文档而基于XML(标准通用标记语言下的一个子集)的语言,用于描述Web Service及其函数、参数和返回值。



webservice CXF总结_xml_02


 

webservice CXF总结_apache_03

  命名空间要相同

 

webservice CXF总结_cxf_04


 

webservice CXF总结_xml_05


 

webservice CXF总结_spring_06


 

webservice CXF总结_cxf_07


 

 

4.CXF实现WebService:

 


• Apache CXF的官方网站如下:


•​​http://cxf.apache.org/​


1.先建立新的web project:


 


webservice CXF总结_spring_08

 

2.导入lib包

 

3. 根据wsdl文件反向生成服务端

 


• CXF提供了这样的工具----wsdl2java


配置CXF环境变量:


CXF_HOME=E:\Java\webservice\apache-cxf-2.7.15,以及在path中增加 %CXF_HOME%/bin


注意:需要jdk 1.7以下,1.8会报错。使用wsdl2java -h可以检测是否成功。


•使用cmd 打开,到cxf/bin下:


webservice CXF总结_apache_09


 

webservice CXF总结_cxf_10


 

然后在bin目录下生成代码:


 


webservice CXF总结_webservice_11


 

 

 


• 然后需要修改一个地方,就是在接口的实现类中把


•wsdlLocation="file:/C:/Users/80575749/Desktop/OnePNS/SoapNotification.wsdl“


•给删除,不然等会运行是会提示找不到这个wsdl文件


 


webservice CXF总结_webservice_12


 

 

4.把生成的代码复制进项目并创建applicationContext.xml配置文件:

webservice CXF总结_cxf_13


webservice CXF总结_cxf_14


 

注意:ScfMobileSoap.java 其实可以删除

 

添加配置内容

webservice CXF总结_xml_15


 

<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">     
<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:endpoint id="ScfPccSoapService"
implementor="com.soap.ScfPccSoapServiceEndpointImpl"
address="/soap" />

</beans>


 

 5.添加web.xml:

 

webservice CXF总结_webservice_16


 

<context-param>           <param-name>contextConfigLocation</param-name>           <param-value>/WEB-INF/applicationContext.xml</param-value>       </context-param>          <listener>           <listener-class>               org.springframework.web.context.ContextLoaderListener  
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>


 

 

6.部署到Tomcat

 

 

 

5.CXF生成Client:

方法与生成Server类似:

在cmd中输入如下命令:

webservice CXF总结_webservice_17


 生成client代码

 

1. 导入lib包,把生成的java文件复制到client项目内


包与接口与服务端相同(只需要写接口)


 


webservice CXF总结_cxf_18


 

Ps:包可以不相同,但是需要修改applicationContext.xml中的bean定义和接口实现类中的注解:endpointInterface的包路径
 

2.配置applicationContext.xml

webservice CXF总结_xml_19

 


<?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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">    <bean id="ScfPccSoap" class="com.soap.ScfPccSoapServiceEndpoint"           factory-bean="clientFactory" factory-method="create"/>    
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.soap.ScfPccSoapServiceEndpoint"/>
<property name="address" value="http://localhost:8080/OnePNSServer/services/soap?wsdl"/>

</bean>

</beans>


 

 

3.写一个Run类来运行客户端:

webservice CXF总结_apache_20


 

webservice CXF总结_webservice_21

 

 4.关闭cxf的log Debug

<logger name="org.apache.cxf" level="warn" />