【WebService】——入门实例
服务端
服务:
1、add(int a,int b)
2、minus(int a,int b)
具体如下:
<pre name="code" class="java">@WebService
public interface IMyService {
//两数相加
public int add(int a, int b);
//两数相减
public int minus(int a, int b);
}
实现类
(注意:引用正确的包)
@WebService(endpointInterface="com.zttc.service.IMyService")
public class MyServiceImpl implements IMyService { @Override
public int add(int a, int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
} @Override
public int minus(int a, int b) {
System.out.println(a+"-"+b+"="+(a-b));
return a-b;
} }
发布服务:
注意:要确保端口号未被占用,发布地址不能重复使用。
//发布地址
String address="http://localhost:5555/service";
//发布
Endpoint.publish(address, new MyServiceImpl()); System.out.println("Service Success!!!");
查看服务:
在浏览器地址栏输入 http://localhost:5555/service?wsdl(自定义的发布地址+"?wsdl"),查看服务。
之后我们需要用到其中的targetNamespace 和name值。
<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://service.zttc.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.zttc.com/" name="MyServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.zttc.com/" schemaLocation="http://localhost:5555/service?xsd=1"/>
</xsd:schema>
</types>
<message name="add">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<message name="minus">
<part name="parameters" element="tns:minus"/>
</message>
<message name="minusResponse">
<part name="parameters" element="tns:minusResponse"/>
</message>
<portType name="IMyService">
<operation name="add">
<input wsam:Action="http://service.zttc.com/IMyService/addRequest" message="tns:add"/>
<output wsam:Action="http://service.zttc.com/IMyService/addResponse" message="tns:addResponse"/>
</operation>
<operation name="minus">
<input wsam:Action="http://service.zttc.com/IMyService/minusRequest" message="tns:minus"/>
<output wsam:Action="http://service.zttc.com/IMyService/minusResponse" message="tns:minusResponse"/>
</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:IMyService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="minus">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MyServiceImplService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:5555/service"/>
</port>
</service>
</definitions>
客户端
调用服务:
1)伪客户端调用
这种调用方式中,调用和被调用者都在同一个java项目中,并不是真正意义上的调用。先在这里介绍下这种方式,其他的在第二种方法中介绍。
try {
//服务地址
URL url=new URL("http://localhost:5555/service?wsdl");
//使用targetNamespace和name
QName sname=new QName("http://service.zttc.com/","MyServiceImplService");//命名空间,提供的名称
Service service=Service.create(url,sname);
//实现接口
IMyService ms=service.getPort(IMyService.class);
System.out.println("1+2="+ms.add(1,2));
} catch (MalformedURLException e) {
e.printStackTrace();
}
2)远程调用——使用wsimport生成服务端
新建一个java project,使用命令wsimport。
语法:wsimport -d 本地存放路径 -keep -verbose wsdl地址
现在将操作之前发布好的服务
在本地目录中可以找到生成的文件,有java文件和class文件。
将wsimport生成的文件拷贝到新的project中,新建Test类,进行测试,代码与1)伪客户端调用类似,不再重复。这样,我们就模拟了远程调用模式。
小结:
今天主要介绍了WebService的入门demo,下篇介绍wsdl的具体配置。
【WebService】——入门实例的更多相关文章
- WebService入门实例教程
什么是WebService 通过使用WebService,您的应用程序可以向全世界发布信息,或提供某项功能,它是基于Web的服务,通过Web进行发布.查找和使用. WebService脚本平台需支持X ...
- Java WebService入门实例
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务. Web Service的关键技术和规则: 1.XML:描述 ...
- webservice入门实例,CXF方式
1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...
- RESTful WebService 入门实例
/* 新建MavenProject,使用以下代码,创建类和POM文件.使用命令行切换到Project根目录,运行mvn package(或者,选中pom.xml 文件右键单击 > run a ...
- React 入门实例教程(转载)
本人转载自: React 入门实例教程
- struts入门实例
入门实例 1 .下载struts-2.3.16.3-all .不摆了.看哈就会下载了. 2 . 解压 后 找到 apps 文件夹. 3. 打开后将 struts2-blank.war ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...
- webservice测试实例
webservice测试实例(LR8.1) 接口声明:这个接口是sina的短信服务接口,我只是用来做脚本学习使用,不会对其产生压力:希望读者也只是用来进行录制学习,而不是产生压力. 接口文档:http ...
- wxPython中文教程入门实例
这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下 wxPython中文教程入门实例 wx.Window 是一个基类 ...
- RESTful WebService入门(转)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/229206 REST ...
随机推荐
- ssm整合实现注册与登录功能
最简洁易懂的SSM整合源码都在这里了 激情提示: 1.本项目是用IDEA编写的,不管你是习惯何种ide工具,那也只是工具而已,源代码才是本质 2.本项目只拥有注册和登录功能,简易的功能和详细的注释,是 ...
- [洛谷]P3704-数字表格
[洛谷]P3704-数字表格 妙啊,这又是一道反演题,而且个人感觉比较高级 传送门 大意 在\(N\times M\)的数表\(a\)中,\(a_{i,j}\)表示f((i,j)),其中\((i,j) ...
- ABAP术语-ABAP Editor
ABAP Editor 原文链接:http://www.cnblogs.com/qiangsheng/archive/2007/12/08/987498.html Program editor in ...
- 通过Ops Manager安装管理mongodb-3.4集群
node1 Ops Manager,mongodb,agent node2 mongodb,agent node3 mongodb,agent 参考文档 https://docs.opsmanager ...
- python3.5怎么打包编译
问题:用Python开发的小工具有时需要编译打包为Windows(*.exe).Mac等操作系统下的可执行性文件以供非程序员使用. 解决方案: 一.py2exe 目前只支持到Python3.4,暂不支 ...
- 剑指offer—从头到尾打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 递归添加...不为空就加 import java.util.ArrayList; public class Solution { pu ...
- nodejs--http
http模块主要用到四个方法: 1.Server类 const http = require('http'); let server = new Server(); server.on('reques ...
- hive自定义函数(UDF)
首先什么是UDF,UDF的全称为user-defined function,用户定义函数,为什么有它的存在呢?有的时候 你要写的查询无法轻松地使用Hive提供的内置函数来表示,通过写UDF,Hive就 ...
- python生成器详解
1. 生成器 利用迭代器(迭代器详解python迭代器详解),我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记 ...
- Java学习笔记八:Java的流程控制语句之循环语句
Java的流程控制语句之循环语句 一:Java循环语句之while: 生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 ...