WebService简介

    xiaoxiao2021-03-25  91

    WebService简介

    最近做公司的一些业务的时候,需要跟一些商家对接,其中有些商家提供的接口使用的是WebService技术,因此学习了一下WebService技术,总结如下。

    一. 简介

    Web Service是一种远程调用技术,它的作用就是从远程系统中获取业务数据。Web Service为组织内部甚至多个组织之间的业务流程的集成提供了一个通用机制,具有特点是开放性,跨平台性。系统与系统之间使用SOAP协议进行通讯。SOAP:即简单对象访问协议, 是使用http发送的XML格式的数据,达到调用远程方法的目的,这样Web Service可以通过HTTP协议与远程机器交互,而且,SOAP更加健壮和灵活易用,但SOAP不是Web Service的专有协议。目前SOAP协议主要有两个版本,SOAP1.1和SOAP1.2。

    二. WebService使用流程 一般来说,使用WebService要先开发服务端,服务端开发完毕后并发布成功之后,会生成服务的描述文档。描述文档采用的是WSDL(web服务描述语言)对服务端接口、方法、参数和返回值,WSDL是随服务发布成功时自动生成,并会提供一个http协议的url可以访问文档。通过阅读文档我们可以进行客户端的开发,首先通过工具根据WSDL文档生成基本的客户端代码,然后开发客户端逻辑来调用服务端的方法。

    下面以Java语言为例,介绍WebService的服务端和客户端的开发流程

    1.创建SEI接口和SEI接口的实现类

    public interface HelloService { String sayHello(String name); } public class HelloServiceImpl implements HelloService { public String sayHello(String name) { String result = "Hello," + name + "!"; System.out.println(result); return result; } }

    2.发布服务:发布服务的方式的主要有以下几种:

    1.Java原生方法:需要在SEI接口类的实现类HelloServiceImpl上加@WebService注解,然后在main入口方法中调用以下代码

    public class HelloServer { public static void main(String[] args) { Endpoint.publish("http://127.0.0.1:12345/hello", new HelloServiceImpl()); } }

    服务发布的地址为 http://127.0.0.1:12345/hello?wsdl,在浏览器打开该地址可以看到服务的wsdl文档,即该服务的使用说明书

    2.Apache CXF发布:需要在SEI接口类HelloService上加@WebService注解,然后在main入口方法中调用以下代码

    public class HelloServer { public static void main(String[] args) { JaxWsServerFactoryBean serverFactoryBean = new JaxWsServerFactoryBean(); serverFactoryBean.setAddress("http://127.0.0.1:12345/hello"); serverFactoryBean.setServiceClass(HelloService.class); serverFactoryBean.setServiceBean(new HelloServiceImpl()); serverFactoryBean.create(); } }

    服务发布的地址为 http://127.0.0.1:12345/hello?wsdl

    3.Spring整合Apache CXF发布

    (1)POM.xml中加入Spring与Apache的依赖

    <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.10</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.10</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.7.RELEASE</version> </dependency>

    (2)在web.xml中加入spring和apache相关servlet

    <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>

    (3)在applicationContext中添加如下配置,并启动web项目,WebService便成功发布

    <jaxws:server address="/hello" serviceClass="com.qipeng.service.HelloService"> <jaxws:serviceBean> <ref bean="helloService"/> </jaxws:serviceBean> </jaxws:server> <bean name="helloService" class="com.qipeng.service.impl.HelloServiceImpl"/>

    此种方式发布的服务的地址规则为:http://ip:端口号/项目名称/servlet拦截路径/服务路径?wsdl 在本例中(假设项目在webapps中的路径为webservice)也就是: http://127.0.0.1:12345/webservice/ws/hello?wsdl

    3.生成客户端代码,有两种方式:

    1.使用jdk自带的wsimport命令,关于wsimport命令的详细说明见官方文档,本例中生成代码的命令为:

    wsimport -keep -p com.demo.client http://localhost:8080/HelloService?wsdl

    2.使用Apach CXF的wsdl2java命令,详细说明见官方文档,本例中的命令为:

    wsdl2java -p com.demo.client http://localhost:8080/HelloService?wsdl

    生成的客户端代码包括HelloServiceImpl.java、HelloServiceImplService.java、ObjectFactory.java、package-info.java、SayHello.java、SayHelloResponse.java,不同的发布方式和命令生成的代码略有不同,但大致上是以上几个类。

    4.编写客户端代码并进行服务调用,与发布服务的方式相对应,调用服务的方式主要也有三种

    1.Java原生方法:先创建服务类HelloServiceImplService的实例,再获取SEI接口的实现类实例,即可调用对应方法:

    public class HelloClient { public static void main(String[] args) { HelloServiceImplService service =new HelloServiceImplService(); HelloServiceImpl helloService = service.getPort(HelloServiceImpl.class); String result = helloService.sayHello("Sven"); System.out.println(result); } }

    2.Apache CXF调用: 创建一个JaxWsProxyFactoryBean的实例,并设置服务类和服务地址,通过这个实例获取SEI接口的实现类实例,即可调用对应方法

    public class CXFHelloClient { public static void main(String[] args) { JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean(); proxyFactoryBean.setServiceClass(HelloService.class); proxyFactoryBean.setAddress("http://127.0.0.1:12345/hello"); HelloService service = proxyFactoryBean.create(HelloService.class); String result = service.sayHello("Sven"); System.out.println(result); } }

    3.Spring整合Apache CXF调用

    (1) 首先在POM.xml中加入Spring与Apache的依赖(同服务端)

    (2) 在applicationContext中加入如下设置

    <?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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="helloClient" address="http://127.0.0.1:8080/ws/hello" serviceClass="com.qipeng.hello.client.HelloService"/> </beans>

    (3) 在主方法中调用:通过Spring上下文容器获得SEI接口的实例,直接调用服务的方法

    public class HelloClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); HelloService helloService = (HelloService) context.getBean("helloClient"); String result = helloService.sayHello("Sven"); System.out.println(result); } }

    上面三种调用方式的输出均为“Hello,Sven!”

    转载请注明原文地址: https://ju.6miu.com/read-23331.html

    最新回复(0)