服务端
1.接口
@WebService
public interface Calculator{
int add(int a,int b);
}
2.实现
@WebService(endpointInterface = "com.demo.testCxf.Calculator",serviceName="calculator")
public vlass CalculatorImpl implements Calculator{
@Override
public int add(int a ,int b){
return a+b;
}
}
3.发布
public class WebService{
public static void main(String[] args){
Calculator cal = new CalculatorImpl();
String address = "http://localhost:8080/calculator";
JaxWsServerFactoryBean jws = new JaxWsServerFactoryBean();
jws.setAddress(address);
jws.setServiceClass(Calculator.class);
jws.setServiceBean(cal);
jws.create();
}
}
客户端
1.生成客户端代码
下载apache-cxf
在bin目录下执行wsdl2java http://localhost:8080/calculator?wsdl
生成代码考入项目
2.编写主类
public class Client{
public static void main(String[] args){
CalculatorService service = new CalculatorService();
Calculator calPort = service.getCalculatorPort();
int add = calPort.add(3,6);
System.out.println(add);
}
}
CXF生成客户端类文件乱码问题
标注编码为国际统一编码utf-8
比如wsdl2java -encoding utf-8 -d 本地目录 -client 你的wsdl地址