服务端



服务:

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】——入门实例的更多相关文章

  1. WebService入门实例教程

    什么是WebService 通过使用WebService,您的应用程序可以向全世界发布信息,或提供某项功能,它是基于Web的服务,通过Web进行发布.查找和使用. WebService脚本平台需支持X ...

  2. Java WebService入门实例

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务. Web Service的关键技术和规则: 1.XML:描述 ...

  3. webservice入门实例,CXF方式

    1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...

  4. RESTful WebService 入门实例

      /* 新建MavenProject,使用以下代码,创建类和POM文件.使用命令行切换到Project根目录,运行mvn package(或者,选中pom.xml 文件右键单击 > run a ...

  5. React 入门实例教程(转载)

    本人转载自: React 入门实例教程

  6. struts入门实例

    入门实例 1  .下载struts-2.3.16.3-all  .不摆了.看哈就会下载了. 2  . 解压  后 找到 apps 文件夹. 3.    打开后将 struts2-blank.war   ...

  7. Vue.js2.0从入门到放弃---入门实例

    最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...

  8. webservice测试实例

    webservice测试实例(LR8.1) 接口声明:这个接口是sina的短信服务接口,我只是用来做脚本学习使用,不会对其产生压力:希望读者也只是用来进行录制学习,而不是产生压力. 接口文档:http ...

  9. wxPython中文教程入门实例

    这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下     wxPython中文教程入门实例 wx.Window 是一个基类 ...

  10. RESTful WebService入门(转)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/229206 REST ...

随机推荐

  1. Linux的开山篇

    一.Linux的学习方向 1.2Linux运维工程师 1.2.2Linux嵌入式开发工程师 1.2.3在Linux下做各种程序开发    javaEE   大数据    Python  PHP  C/ ...

  2. 一、spring 4概述

    0 前言 0.0 Spring 来历 Spring 是于2003年兴起的一个轻量级的Java 开发框架, 为了解决企业应用开发的复杂性而创建, 核心是控制反转(IoC)和面向切面编程(AOP). 简单 ...

  3. p标签内不能含有块元素。

    原来一直听网上这样说.自己并没有实际遇到过.上例子. <!DOCTYPE html> <html> <head> <meta charset="ut ...

  4. 服务器空间不足导致mysql服务器无法运行

    今天有朋友请我帮忙解决一个问题,他公司服务器mysql数据库一直连接失败.登录服务期之后发现服务器空间占满了,导致mysql不能启动. 下面说解决方法: 首先查看空间占用,发现空间占满了 df -h ...

  5. python继续函数-练习(2017-8-3)

    写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 def detection(p): intcount = 0 strcount = 0 othercount = 0 spa ...

  6. YUM工具使用

    一.yum命令概述: 1.简介: yum命令时在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载R ...

  7. ESP32 学习笔记 - 环境搭建

    打开终端 输入命令 sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-seria ...

  8. Shoot the Bullet(ZOJ3229)(有源汇上下界最大流)

    描述 ensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utop ...

  9. Entity Framework 数据生成选项DatabaseGenerated【转】

    在EF中,我们建立数据模型的时候,可以给属性配置数据生成选项DatabaseGenerated,它后有三个枚举值:Identity.None和Computed. Identity:自增长 None:不 ...

  10. SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误

    今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...