首页 > 代码库 > JavaEE 之 WebService
JavaEE 之 WebService
1.WebService
a.定义:WebService是一种跨编程语言和跨操作系统平台的远程调用技术
b.三大技术: XML+XSD,SOAP,WSDL
c.SOAP协议 = HTTP协议 + XML数据格式
d.WSDL:基于XML的描述语言(相当于字典、菜单)
2.服务端:
a.在xml中配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF框架的核心Servlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/server/*</url-pattern> </servlet-mapping>
b.建spring-webservice.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> <bean id="webserviceServer" class="com.wode.server.impl.MyWebServiceImpl" /> <jaxws:endpoint id="myService" implementor="#webserviceServer" address="/HelloService"> </jaxws:endpoint> </beans>
c.建接口
@WebService public interface MyWebService { int add(@WebParam(name = "firstA") int a, @WebParam(name = "firstB") int b); int minus(@WebParam(name = "secondA") int a, @WebParam(name = "secondB") int b); }
d.建实现类
@WebService(endpointInterface = "com.wode.server.MyWebService") public class MyWebServiceImpl implements MyWebService { @Override public int add(int a, int b) { // TODO Auto-generated method stub System.out.println(a+b); return a+b; } @Override public int minus(int a, int b) { // TODO Auto-generated method stub System.out.println(a-b); return a-b; } }
e.测试(需先运行tomcat服务器tomcat:run)
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); 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=new MyWebServiceImplService(); MyWebService myWebService=service.getMyWebServiceImplPort(); System.out.println(myWebService.add(10, 100)+"client");
JavaEE 之 WebService
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。