包名
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 <?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
4.web.xml <?xml version="1.0" encoding="UTF-8"?>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">
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/*
index.jsp
在浏览器中输入:http://localhost:8080/webws/Cxf?wsdl 窗口出现一大堆xml内容,及说明服务器端配置成功 仔细浏览xml内容,能够找到我们写的sayHello和sayBye方法名。 二、客户端(方法一) 1.新建一个普通的WEB工程,不需要是Web Service 2.spring.xml <?xml version="1.0" encoding="UTF-8"?>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd">
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();
factory.setAddress("http://localhost:8080/webws/Cxf");
factory.setServiceClass(ICxf.class);
ICxf cxf = (ICxf) factory.create();
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包 欧拉......