- 创建java项目
-
引入所有jar包
-
创建服务类
package com.eetrust.cxf.server;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
@WebService
//制定发布soap 1.2
//@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING)
public class HelloService {
public String sayHello(String name){
return name +" hello";
}
}
4.发布服务
package com.eetrust.cxf.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MyCXFServer {
public static void main(String[] args) {
// 创建服务工厂对象
// ServerFactoryBean sfb = new ServerFactoryBean(); 不推荐使用
JaxWsServerFactoryBean sfb = new JaxWsServerFactoryBean();
sfb.getInInterceptors().add(new LoggingInInterceptor());
sfb.getOutInterceptors().add(new LoggingOutInterceptor());
// 制定服务地址
sfb.setAddress("http://127.0.0.1:8888/hello");
// 绑定服务类
sfb.setServiceClass(HelloService.class);
// 设置服务类的实例对象
sfb.setServiceBean(new HelloService());
// 发布服务
sfb.create();
System.out.println("server ready........");
}
}
5.1
通过wsimport
(
SOAP1.1
)生成客户端代码
如:wsimport -s . http://localhost:8888/hello?wsdl
命令参数说明:
-d:生成客户端执行类的class文件的存放目录
-s:生成客户端执行类的源文件的存放目录
-p:定义生成类的包名
5.2 通过
wsdl2java(SOAP1.1)生成客户端代码
如:wsdl2java –s . http://127.0.0.1:6666/helloworld?wsdl
点表示当前目录 url
6.
调用
webservice4
package com.eetrust.cxf.client;
import com.eetrust.cxf.server.HelloService;
import com.eetrust.cxf.server.HelloServiceService;
public class SoapClient {
public static void main(String[] args) {
HelloServiceService hss = new HelloServiceService();
HelloService hs = hss.getHelloServicePort();
String sayHello = hs.sayHello("lisi");
System.out.println(sayHello);
}
}