Webservice接口的生成及调用

最近项目上要对接一个Webservice形式的接口,因为以前一直没有对接过这种类型的,所以这次专门查了一些资料学习下

一、Webservice的简单介绍

WebService是一种跨编程语言和跨操作系统平台的远程调用技术。它通过标准通信协议,在互联网上以服务的方式发布有用的程序模块,目前大部分是用SOAP作为通信协议。它提供一份详细的接口说明书,来帮助用户构建应用程序,这个接口说明书叫WSDL(Web服务描述语言,Web Service Description Language)。还有就是它请求报文和返回报文都是XML格式的。

优点:
1.跨编程语言和跨操作系统,通俗点说就是只要你网络是通的,你.net开发的东西,我java语言也可以调用,跟操作系统也没关系。

缺点:
1.服务端接口方为webservice则客户端也必须使用webservice。
2.因为webservice使用xml传输数据,因此性能上不能满足高并发

二、Webservice的服务端发布

1.发布web程序服务端发布 (这块后续单独写一个模块)

2.本地发布(这个其实很简单,就是写一个测试类运行一下就行,我直接贴代码)

package com.niu;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class TestWebservice {
    /**
     * 供客户端调用的方法
     * @param name
     * @return
     */
    public String getvalue(String name) {
        return "我叫"+name;
    }
    public static void main(String[] args) {
        //用于发布服务,第一个参数是服务地址,端口号需要未被占用的。第二个参数为本类
        Endpoint.publish("http://localhost:9002/Service/TestService",
        new TestWebservice());
        System.out.println("service succes");
    }
}

三、Webservice的代码生成

1.用dos命令生成代码(在idea中可以直接打开终端然后执行wsimport命令,前提是安装了jdk)

(1)wsimport -keep http://xxxx/xxx/xxx.asmx?wsdl

(2)wsimport -s d:\java\src -p com.nrj.client -keep http://xxxx/xxx/xxx.asmx?wsdl

         wsimport -s  文件路径 -p  包名 -keep http://xxxx/xxx/xxx.asmx?wsdl

 2.idea生成(直接右键open in terminal打开终端)

四、Webservice的客户端调用

1.简单调用(也是直接写个简单测试类,同时把生成的代码也放在项目中,然后运行就行)

package com.niu.ws;

import com.niu.TestWebservice;
import com.niu.TestWebserviceService;

public class ServiceTest {
    public static void main(String[] args) {
        TestWebservice ws = new TestWebserviceService().getTestWebservicePort();
        String name = ws.getvalue("张三");
        System.out.println("输出:"+name);
    }
}

2.service编程实现调用

3.利用apache的AXIS直接调用远程的web service

4.HttpURLConnection调用方式

5.Ajax调用方式

五、wsdl的xml接口说明书

<service>    服务视图,webservice的服务结点,它包括了服务端点
<binding>     为每个服务端点定义消息格式和协议细节
<portType>   服务端点,描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType
<message>   定义一个操作(方法)的数据参数(可有多个参数)
<types>        定义 web service 使用的全部数据类型

<!--  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.  -->
<!--  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.  -->
<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://niu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://niu.com/" name="TestWebserviceService">
<types>
<xsd:schema>
<xsd:import namespace="http://niu.com/" schemaLocation="http://localhost:9002/Service/ServiceHello?xsd=1"/>
</xsd:schema>
</types>
<message name="getvalue">
<part name="parameters" element="tns:getvalue"/>
</message>
<message name="getvalueResponse">
<part name="parameters" element="tns:getvalueResponse"/>
</message>
<portType name="TestWebservice">
<operation name="getvalue">
<input wsam:Action="http://niu.com/TestWebservice/getvalueRequest" message="tns:getvalue"/>
<output wsam:Action="http://niu.com/TestWebservice/getvalueResponse" message="tns:getvalueResponse"/>
</operation>
</portType>
<binding name="TestWebservicePortBinding" type="tns:TestWebservice">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getvalue">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="TestWebserviceService">
<port name="TestWebservicePort" binding="tns:TestWebservicePortBinding">
<soap:address location="http://localhost:9002/Service/ServiceHello"/>
</port>
</service>
</definitions>

自我分析:
1.服务端的类及方法:

TestWebservice ---->getvalue(String name)

2.客户端的使用:
(1)portType标签:portType中的name是原先的服务端类名,operation的name是服务端类名下的方法。
    TestWebservice ----->getvalue
(2)service标签:service中的name是服务端类名的实现类,port是可以获取该实现类的实例
    TestWebserviceService ---->TestWebservicePort
3.例子:
    TestWebservice ws = new TestWebserviceService().getTestWebservicePort();
    String name = ws.getvalue("张三");
4.总结:
  portType是接口标签
  service是实现类标签

六、常用的Webservice接口地址

IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据)
Endpoint :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
Disco         :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl

随机英文、数字和中文简体字 WEB 服务
Endpoint :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl

中国邮政编码 <-> 地址信息双向查询/搜索 WEB 服务
Endpoint :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?disco
WSDL      :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?wsdl

附参考链接

常用Webservice接口地址

https://blog.csdn.net/qq_41694906/article/details/88029533

关于 webservice客户端生成代码中的 JAXBElement <T> 字段类型的解决办法

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值