??xml version="1.0" encoding="utf-8" standalone="yes"?>
开发环境:
1. jdk1.5
2. CXF框架Q版?span style="font-family: Helvetica; color: black; font-size: 14pt;">apache-cxf-2.2.3.zipQ到http://cxf.apache.org/download.html下蝲
注:如?span style="color: red;">jdk1.6q行开发,需下蝲jaxb-api.jar?span style="color: red;">jaxws-api.jarQ然后在本机安装JDK的地方,?span style="color: red;">jdk1.6.0?span style="color: red;">jre文g夹下?span style="color: red;">lib文g夹中新徏endorsed文g夹,攑օ以上两个jar包才可以q行开?/strong>?/span>
W一步,先在MyEclipse新徏一?/strong>java目Q项目名?/strong>HelloWebService?/span>
W二步,在项目中引入apache-cxf-2.2.3.zip?/strong>lib下的所?/strong>jar包?/strong>
W三步,~写试用接口以及其实现c:
接口Q?/span>
实现c:
在接口中dWebService的注解,其标注?span style="font-size: 14pt;">WebService的服务接口?/p>
W四步,~写WebService的服务器端?/strong>
factory.setAddress("http://localhost:8080/HelloWebService");
讄服务在服务器上部|的位置
factory.setServiceClass(HelloImpl.class);
讄服务暴露的接口实现类
完成之后q行MainServer中的mainҎ?/p>
注:因ؓCXF框架中有Jetty 6 服务器,所以这个的demo发布在其中运行?/p>
之后打开览器,输入Q?/p>
http://localhost:8080/HelloWebService?wsdl
如能看见以下画面?span style="font-size: 14pt;">WebService发布成功Q?/p>
W五步,~写客户?/strong>
factory.setServiceClass(Hello.class);
讄讉K服务器端的指定接口?/p>
factory.setAddress("http://localhost:8080/HelloWebService");
讄讉K的服务的地址?/p>
factory.create()
创徏代理对象以供q程调用
之后q行Client?span style="font-size: 14pt;">mainҎQ可以在控制台的服务器端看见如下输出Q?/p>
说明客户端调?span style="font-size: 14pt;">WebService成功?/p>
xQ这个简单的WebService开发完?br />
试qjdk1.6 它的包会变成import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; q且没有factory.setAddressq个Ҏ 采用自带的例子进行测?会出现参数null错误Q但是客L调用服务器端都调用成功,可能原因是没有加载那两个JAR文g的问题,暂时没有试
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HelloWorld {
public OMElement sayHello(OMElement in){
String name=in.getText();
String info=name+"HelloWorld!";
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
resp.setText(info);
return resp;
}
}
|
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorld">
<description>
This is a sample Web Service.
</description>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
|
package example.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class TestClient {
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
OMElement method=fac.createOMElement("sayHello",omNs);
method.setText("ZJ");
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=TestClient.getSayHelloOMElement();
OMElement result=sender.sendReceive(sayHello);
System.out.println(result);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
|
package sample;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HelloWorld {
//dclient?/span>getSayHelloOMElement()Ҏ传递的参数in?/span>
public OMElement sayHello(OMElement in){
//?/span>in转换?/span>String?/span>
String name=in.getText();
String info=name+"HelloWorld!";
//创徏response SOAP包?/span>
OMFactory fac=OMAbstractFactory.getOMFactory();
// OMNamespace指定?/span>SOAP文档名称I间?/span>
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
//创徏元素sayHelloQƈ指定其在omNs指代的名U空间中?/span>
OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
//指定元素的文本内宏V?/span>
resp.setText(info);
return resp;
}
}
|
<?xml version="1.0" encoding="UTF-8"?>
//下面定义服务?/span>
<service name="HelloWorld">
<description>
This is a sample Web Service.
</description>
// ServiceClass指定Java Class的位|,卛_现服务的cR?/span>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
// operation ?/span>Java Class中方法名对应?/span>
<operation name="sayHello">
// messageReceiver看下文注解?/span>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
|
package example.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class TestClient {
// targetEPR指定打包?/span>ServiceQ?/span>.aar文gQ?/span>在容器中的物理位|?/span>
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
//创徏request SOAP包?/span>
OMFactory fac=OMAbstractFactory.getOMFactory();
// OMNamespace指定?/span>SOAP文档名称I间?/span>
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
//创徏元素sayHelloQƈ指定其在omNs指代的名U空间中?/span>
OMElement method=fac.createOMElement("sayHello",omNs);
//指定元素的文本内宏V?/span>
method.setText("ZJ");
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=TestClient.getSayHelloOMElement();
//发出request SOAPQ?/span>
//同时得到的q端?/span>sayHelloҎq回的信息保存到result?/span>
//通过services.xml能准找?/span>sayHelloҎ所在的文g?/span>
OMElement result=sender.sendReceive(sayHello);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
|