1.WebService
a.定义:WebService是一种跨编程语言和跨操作系统平台的远程调用技术
b.三大技术: XML+XSD,SOAP,WSDL
c.SOAP协议 = HTTP协议 + XML数据格式
d.WSDL:基于XML的描述语言(相当于字典、菜单)
2.服务端:
a.在xml中配置
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
2
CXFServlet
/server/*
b.建spring-webservice.xml
c.建接口
@WebServicepublic interfaceMyWebService {int add(@WebParam(name = "firstA") int a, @WebParam(name = "firstB") intb);int minus(@WebParam(name = "secondA") int a, @WebParam(name = "secondB") intb);
}
d.建实现类
@WebService(endpointInterface = "com.wode.server.MyWebService")public class MyWebServiceImpl implementsMyWebService {
@Overridepublic int add(int a, intb) {//TODO Auto-generated method stub
System.out.println(a+b);return a+b;
}
@Overridepublic int minus(int a, intb) {//TODO Auto-generated method stub
System.out.println(a-b);return a-b;
}
}
e.测试(需先运行tomcat服务器tomcat:run)
JaxWsProxyFactoryBean factoryBean = newJaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyWebService.class);
factoryBean.setAddress("http://127.0.0.1:8080/myService/server/HelloService");
MyWebService service=(MyWebService) factoryBean.create();
System.out.println(service.add(1, 3));
f.访问http://127.0.0.1:8080/myService/server/HelloService?wsdl确认是否正常
g.下载apache-cxf-3.1.11并配置环境变量
h.在控制台输入
wsdl2java -encoding utf-8 -d E:\EclipseWorkspace\myService http://127.0.0.1:8080/myService/server/HelloService?wsdl
i.将项目下com文件夹复制到客户端项目中
3.客户端
a.使用
MyWebServiceImplService service=newMyWebServiceImplService();
MyWebService myWebService=service.getMyWebServiceImplPort();
System.out.println(myWebService.add(10, 100)+"client");