WebService

  • 什么是webservice:webservice也可以叫 xml web service webservice,轻量级的独立的通讯技术
  1. 基于web的服务:服务端提供的服务接口让客户端访问
  2. 跨平台,跨语言的整合方案
  • 为什么要使用webservice:跨语言调用的解决方案
  • 什么时候要去使用webservice:列如:电商平台中包含的订单物流信息状态,我们需要调用第三方物流系统的接口查询物流信息,但是第三方物流系统是由.net实现的webservice服务接口,我们本身是java语言写的电商平台,那么这时候就可以使用webservice调取第三方服务。
  • webservice基本概念:WSDL(web service definition language) :webservice定义语言。webservice服务需要通过wsdl文件来说明自己有什么服务可以对外调用。并且有哪些方法、方法里面有哪些参数。wsdl基于XML(可扩展标记语言)去定义的。
  1. 对应一个.wsdl的文件类型。
  2. 定义了webservice的服务器端和客户端应用进行交互的传递数据和响应数据格式和方式。
  3. 一个webservice对应唯一一个wsdl文档。
  • SOAP(simple object access protocal):简单对象访问协议(http+xml)。webservice通过http协议发送和接收请求时,发送的内容(请求报文)和接收的内容(响应报文)都是采用xml格式进行封装。这些特定的HTTP消息头和XML内容格式就是SOAP协议。
  1. 一种简单,基于HTTP和XML的协议。
  2. soap消息:请求和响应消息
  3. http+xml报文
  • SEI(webservice endpoint interface ):webservice的终端接口。webservice服务端用来处理请求的接口,也就是发布出去的接口。
  • 简单实现一个webservice
    • 服务端:
    • 新建一个接口实现输出入参: 
 1 package com.karat.cn;
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 
 6 @WebService//SE和SEI的实现类
 7 public interface HelloWord {
 8 
 9     @WebMethod//SEI中的方法
10     public String say(String name);
11 }
View Code
    • 新建一个接口实现该接口的实现类:
 1 package com.karat.cn;
 2 
 3 import javax.jws.WebService;
 4 
 5 @WebService
 6 public class HelloWordImpl implements HelloWord{
 7 
 8     @Override
 9     public String say(String name) {
10         // TODO Auto-generated method stub
11         System.out.println("call say()");
12         return "helloword"+name+"i'm word";
13     }
14 
15 }
View Code
    • 新建启动项:
 1 package com.karat.cn;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 public class Bootstrap {
 6 
 7     public static void main(String[] args){
 8         //发布接口地址
 9         Endpoint.publish("http://localhost:8080/webservice/say", new HelloWordImpl());
10         
11         System.out.println("publish success");
12     }
13 }
View Code
    • pom文件引入依赖:
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.karat.cn</groupId>
 5   <artifactId>webservice</artifactId>
 6   <packaging>war</packaging>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <name>webservice Maven Webapp</name>
 9   <url>http://maven.apache.org</url>
10   <dependencies>
11      <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17     <dependency>
18       <groupId>org.apache.zookeeper</groupId>
19       <artifactId>zookeeper</artifactId>
20       <version>3.4.8</version>
21     </dependency>
22     <dependency>
23       <groupId>com.101tec</groupId>
24       <artifactId>zkclient</artifactId>
25       <version>0.10</version>
26     </dependency>
27 
28     <dependency>
29       <groupId>org.apache.curator</groupId>
30       <artifactId>curator-framework</artifactId>
31       <version>2.11.0</version>
32     </dependency>
33 
34     <dependency>
35       <groupId>org.apache.curator</groupId>
36       <artifactId>curator-recipes</artifactId>
37       <version>2.11.0</version>
38     </dependency>
39 
40   </dependencies>
41   <build>
42      <finalName>webservice</finalName>
43      <plugins>
44             <plugin>
45                 <groupId>org.apache.maven.plugins</groupId>
46                 <artifactId>maven-compiler-plugin</artifactId>
47                 <configuration>
48                     <source>1.8</source>
49                     <target>1.8</target>
50                 </configuration>
51             </plugin>
52      </plugins>
53   </build>
54 </project>
View Code
    • 启动main方法发布成功后在网页端输入:http://localhost:8080/webservice/say?wsdl后可得到wsdl文档如下:
 1 <!--
 2  Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
 3 -->
 4 <!--
 5  Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
 6 -->
 7 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cn.karat.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://cn.karat.com/" name="HelloWordImplService">
 8 <types>
 9 <xsd:schema>
10 <xsd:import namespace="http://cn.karat.com/" schemaLocation="http://localhost:8080/webservice/say?xsd=1"/>
11 </xsd:schema>
12 </types>
13 <message name="say">
14 <part name="parameters" element="tns:say"/>
15 </message>
16 <message name="sayResponse">
17 <part name="parameters" element="tns:sayResponse"/>
18 </message>
19 <portType name="HelloWordImpl">
20 <operation name="say">
21 <input wsam:Action="http://cn.karat.com/HelloWordImpl/sayRequest" message="tns:say"/>
22 <output wsam:Action="http://cn.karat.com/HelloWordImpl/sayResponse" message="tns:sayResponse"/>
23 </operation>
24 </portType>
25 <binding name="HelloWordImplPortBinding" type="tns:HelloWordImpl">
26 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
27 <operation name="say">
28 <soap:operation soapAction=""/>
29 <input>
30 <soap:body use="literal"/>
31 </input>
32 <output>
33 <soap:body use="literal"/>
34 </output>
35 </operation>
36 </binding>
37 <service name="HelloWordImplService">
38 <port name="HelloWordImplPort" binding="tns:HelloWordImplPortBinding">
39 <soap:address location="http://localhost:8080/webservice/say"/>
40 </port>
41 </service>
42 </definitions>
View Code
    • 客户端:
    • 通过cmd进入到当前项目demo目录下,在cmd中执行wsimport -keep http://localhost:8080/webservice/say?wsdl如下:

    • 在同级目录下生成一个com文件夹,内部包含文件如下:

    • 将.class文件以及-info.class说明文件去除,引入.java文件到客户端项目中。
    • 新建一个web项目,将生成的com包直接copy到新建的服务端项目中

    • 创建服务端启动项,代码如下:
 1 package com.karat.cn.service;
 2 
 3 public class Demo {
 4 
 5     public static void main(String args[]){
 6         HelloWordImplService service=new HelloWordImplService();
 7         HelloWordImpl helloWordImpl=service.getHelloWordImplPort();
 8         System.out.print(helloWordImpl.say("123"));
 9     }
10 }
View Code
原文地址:https://www.cnblogs.com/LJing21/p/10449561.html