利用CXF开发WebService的小案例
http://www.blogjava.net/ashutc/archive/2009/11/24/303521.html开发工具:MyEclipse 6.0
开发环境:
1. jdk1.5
2. CXF框架,版本apache-cxf-2.2.3.zip,到http://cxf.apache.org/download.html下载
注:如使用jdk1.6进行开发,需下载jaxb-api.jar和jaxws-api.jar,然后在本机安装JDK的地方,在jdk1.6.0的jre文件夹下的lib文件夹中新建endorsed文件夹,放入以上两个jar包才可以进行开发。
第一步,先在MyEclipse新建一个java项目,项目名为HelloWebService。
第二步,在项目中引入apache-cxf-2.2.3.zip中lib下的所有jar包。
第三步,编写测试用接口以及其实现类:
接口:
Java代码
[*]package test;
[*]
[*]import javax.jws.WebService;
[*]
[*]public interface Hello {
[*]
[*] public String sayHello(String str);
[*]
[*]}
package test;
import javax.jws.WebService;
public interface Hello {
public String sayHello(String str);
}
实现类:
Java代码
[*]package test;
[*]
[*]public class HelloImpl implements Hello {
[*]
[*] public String sayHello(String str) {
[*]
[*] System.out.println("调用成功");
[*] return "Hello " + str;
[*] }
[*]
[*]}
package test;
public class HelloImpl implements Hello {
public String sayHello(String str) {
System.out.println("调用成功");
return "Hello " + str;
}
}
在接口中添加WebService的注解,将其标注为WebService的服务接口。
Java代码
[*]@WebService
[*]public interface Hello {
@WebService
public interface Hello {
第四步,编写WebService的服务器端。
Java代码
[*]package test;
[*]
[*]import org.apache.cxf.endpoint.Server;
[*]import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
[*]
[*]public class MainServer {
[*]
[*] public static void main(String[] args) {
[*] JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
[*] factory.setAddress("http://localhost:8080/HelloWebService");
[*] factory.setServiceClass(HelloImpl.class);
[*] Server server = factory.create();
[*] server.start();
[*] }
[*]}
package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:8080/HelloWebService");
factory.setServiceClass(HelloImpl.class);
Server server = factory.create();
server.start();
}
}
factory.setAddress("http://localhost:8080/HelloWebService");
设置服务在服务器上部署的位置
factory.setServiceClass(HelloImpl.class);
设置服务暴露的接口实现类
完成之后运行MainServer中的main方法。
注:因为CXF框架中有Jetty 6 服务器,所以这个的demo发布在其中运行。
之后打开浏览器,输入:
http://localhost:8080/HelloWebService?wsdl
如能看见以下画面则WebService发布成功:
Xml代码
[*]<?xml version="1.0" encoding="UTF-8" ?>
[*]- <wsdl:definitions name="HelloImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
[*]- <wsdl:types>
[*]- <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0" xmlns:tns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
[*]<xs:element name="sayHello" type="tns:sayHello" />
[*]<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
[*]- <xs:complexType name="sayHello">
[*]- <xs:sequence>
[*]<xs:element minOccurs="0" name="arg0" type="xs:string" />
[*]</xs:sequence>
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0" xmlns:tns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xs:complexType name="sayHello">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
</xs:sequence>
第五步,编写客户端
Java代码
[*]package test;
[*]
[*]import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
[*]
[*]public class Client {
[*] public static void main(String[] args) {
[*] JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
[*] factory.setServiceClass(Hello.class);
[*] factory.setAddress("http://localhost:8080/HelloWebService");
[*] Hello hello = (Hello)factory.create();
[*]
[*] System.out.println(hello.sayHello("weberyb"));
[*] }
[*]
[*]}
package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/HelloWebService");
Hello hello = (Hello)factory.create();
System.out.println(hello.sayHello("weberyb"));
}
}
factory.setServiceClass(Hello.class);
设置访问服务器端的指定接口。
factory.setAddress("http://localhost:8080/HelloWebService");
设置访问的服务的地址。
factory.create()
创建代理对象以供远程调用
之后运行Client中main方法,可以在控制台的服务器端看见如下输出:
Java代码
[*]....................
[*]2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
[*]信息: Creating Service {http://test/}HelloService from class test.Hello
[*]Hello weberyb
....................
2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}HelloService from class test.Hello
Hello weberyb
说明客户端调用WebService成功。
至此,这个简单的WebService开发完毕 尝试过jdk1.6 它的包会变成import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; 并且没有factory.setAddress这个方法 采用自带的例子进行测试 会出现参数null错误,但是客户端调用服务器端都调用成功,可能原因是没有加载那两个JAR文件的问题,暂时没有测试
页:
[1]