1、根据服务端发布的http://localhost:8080/autoAnalysis/ws/WService?wsdl,如下图:
2、编写客户端接口
创建ClientServer.java,代码如下:
package com.cah.ddi3.ws;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace="http://ws.ddi3.cah.com/")
public interface HelloWebService {
@WebResult(name="return", targetNamespace="http://ws.ddi3.cah.com/")
String sayHello(@WebParam(name = "name") String name);
@WebResult(name="return", targetNamespace="http://ws.ddi3.cah.com/")
String saveInfo(@WebParam(name = "name") String name,@WebParam(name = "info") String info);
}
3、编写客户端调用方
创建ClientCall.java,代码如下:
package com.cah.ddi3.ws;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class HelloClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8080/autoAnalysis/ws/WService");
factory.setServiceClass(HelloWebService.class);
HelloWebService helloWebService = (HelloWebService) factory.create();
String result = helloWebService.sayHello("小鱼儿");
System.out.println(result);
String saveResult = helloWebService.saveInfo("小鱼儿", "欢迎你!");
System.out.println(saveResult);
}
}
4、运行代码
控制台会打印如下内容,说明调用成功: