WebService-CXF_配置文件篇

来源:互联网 发布:手工编程的步骤 编辑:程序博客网 时间:2024/05/22 09:48

CXF的下载安装,环境变量的配置,jar包的添加,就不多说了


一、服务器端
先上组织结构图:


1.新建一个普通的WEB工程,不需要是Web Service
2.Java类
package pro.webws.client;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface ICxf {public String sayHello(@WebParam(name = "arg0") String name);public String sayBye(@WebParam(name = "arg0") String name);}
接口实现
package pro.webws.client;import javax.jws.WebService;@WebService(endpointInterface = "pro.webws.client.ICxf")public class CxfImpl implements ICxf {public String sayHello(String name) {return "Hello:" + name;}public String sayBye(String name) {return "ByeBye:" + name;}}
3.配置文件(主要添加的已红色标注)
applicationContext.xml <id任意>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
<span style="white-space:pre"></span>xmlns:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans      <span style="white-space:pre"></span> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       <span style="white-space:pre"></span>http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:endpoint id="abc" implementor="pro.webws.client.CxfImpl" address="/Cxf" /></beans>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath: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>/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
在浏览器中输入:http://localhost:8080/webws/Cxf?wsdl
窗口出现一大堆xml内容,及说明服务器端配置成功
仔细浏览xml内容,能够找到我们写的sayHello和sayBye方法名。

二、客户端(方法一)


1.新建一个普通的WEB工程,不需要是Web Service
2.spring.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
<span style="white-space:pre"></span>xmlns:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans      <span style="white-space:pre"></span> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     <span style="white-space:pre"></span> http://cxf.apache.org/jaxws       <span style="white-space:pre"></span> http://cxf.apache.org/schema/jaxws.xsd"><span style="white-space:pre"></span><bean id="client" class="pro.webws.client.ICxf" factory-bean="clientFactory" factory-method="create" />               <span style="white-space:pre"></span><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">      <span style="white-space:pre"></span>    <property name="serviceClass" value="pro.webws.client.ICxf" />      <span style="white-space:pre"></span>    <property name="address" value="http://localhost:8080/webws/Cxf" />        <span style="white-space:pre"></span></bean></beans>
3.Java类
ICxf.java与服务器端的接口一样,直接复制粘贴
测试类Test.java
package pro.webws.client;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");ICxf cxf= (ICxf) ctx.getBean("client");String name = (String) cxf.sayHello("Tom");System.out.println(name);String name2 = (String) cxf.sayBye("Jerry");System.out.println(name2);}}
输出结果:



注:运行测试时,若报错,就将客户端的jcl-over-slf4j-1.7.7.jar该架包删除
若还报错,就根据提示百度找结果吧

客户端(方法二)<编程式,不用配置文件>
1.接口类,ICxf.java与服务器端的接口一样,直接复制粘贴
2.测试类 ,Test.java
package pro.webws.client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Test {public static void main(String[] args) {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();          <span style="white-space:pre"></span>factory.setAddress("http://localhost:8080/webws/Cxf");          <span style="white-space:pre"></span>factory.setServiceClass(ICxf.class);          <span style="white-space:pre"></span>ICxf cxf = (ICxf) factory.create();          <span style="white-space:pre"></span>String name = (String) cxf.sayHello("Tom");System.out.println(name);String name2 = (String) cxf.sayBye("Jerry");System.out.println(name2);}}
3.jar包(精简版)

最精简的jar包,如果使用到CXF其他功能,自行添加jar包



欧拉......














0 0