1.定义接口

 package org.WebService.ws.annotation;

 import javax.jws.WebService;

 @WebService
public interface ICalculator {
float add(float a, float b);
float minus(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
}

2.服务接口实现类

 package org.WebService.ws.annotation;

 import javax.jws.WebService;
//endpointInterface指定接入点接口:接口必须存在 
@WebService(endpointInterface="org.WebService.ws.annotation.ICalculator")
public class CalculatorImpl implements ICalculator { @Override
public float add(float a, float b) {
// TODO Auto-generated method stub
System.out.println("a+b="+(a+b));
return a + b;
} @Override
public float minus(float a, float b) {
// TODO Auto-generated method stub
System.out.println("a-b="+(a-b));
return a - b;
} @Override
public float multiply(float a, float b) {
// TODO Auto-generated method stub
return a * b;
} @Override
public float divide(float a, float b) {
// TODO Auto-generated method stub
return a / b;
} }

3.发布服务

直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:

 package org.WebService.ws.annotation;

 import javax.xml.ws.Endpoint;

 public class Server {

     public static void main(String[] args) {
// TODO Auto-generated method stub
String address="http://localhost:9998/hw";
Endpoint.publish(address, new CalculatorImpl());
} }

浏览器地址栏输入:访问webservice看看是否发布成功【地址后面加上"?wsdl"】:

http://localhost:9998/hw?wsdl

浏览器显示如下:

 This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<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://annotation.ws.WebService.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://annotation.ws.WebService.org/" name="CalculatorImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://annotation.ws.WebService.org/" schemaLocation="http://localhost:9998/hw?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="multiply">
<part name="parameters" element="tns:multiply"/>
</message>
<message name="multiplyResponse">
<part name="parameters" element="tns:multiplyResponse"/>
</message>
<message name="divide">
<part name="parameters" element="tns:divide"/>
</message>
<message name="divideResponse">
<part name="parameters" element="tns:divideResponse"/>
</message>
<message name="minus">
<part name="parameters" element="tns:minus"/>
</message>
<message name="minusResponse">
<part name="parameters" element="tns:minusResponse"/>
</message>
<portType name="ICalculator">
<operation name="add">
<input wsam:Action="http://annotation.ws.WebService.org/ICalculator/addRequest" message="tns:add"/>
<output wsam:Action="http://annotation.ws.WebService.org/ICalculator/addResponse" message="tns:addResponse"/>
</operation>
<operation name="multiply">
<input wsam:Action="http://annotation.ws.WebService.org/ICalculator/multiplyRequest" message="tns:multiply"/>
<output wsam:Action="http://annotation.ws.WebService.org/ICalculator/multiplyResponse" message="tns:multiplyResponse"/>
</operation>
<operation name="divide">
<input wsam:Action="http://annotation.ws.WebService.org/ICalculator/divideRequest" message="tns:divide"/>
<output wsam:Action="http://annotation.ws.WebService.org/ICalculator/divideResponse" message="tns:divideResponse"/>
</operation>
<operation name="minus">
<input wsam:Action="http://annotation.ws.WebService.org/ICalculator/minusRequest" message="tns:minus"/>
<output wsam:Action="http://annotation.ws.WebService.org/ICalculator/minusResponse" message="tns:minusResponse"/>
</operation>
</portType>
<binding name="CalculatorImplPortBinding" type="tns:ICalculator">
<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="multiply">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="divide">
<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="CalculatorImplService">
<port name="CalculatorImplPort" binding="tns:CalculatorImplPortBinding">
<soap:address location="http://localhost:9998/hw"/>
</port>
</service>
</definitions>

4.创建客户端

 package org.WebService.ws.annotation;

 import java.net.MalformedURLException;
import java.net.URL; import javax.xml.namespace.QName;
import javax.xml.ws.Service; public class Client { public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
URL url=new URL("http://localhost:9998/hw?wsdl");
QName qname = new QName("http://annotation.ws.WebService.org/", "CalculatorImplService");
Service service = Service.create(url, qname);
ICalculator cal = service.getPort(ICalculator.class);
cal.add(1, 2);
cal.minus(2, 1); } }

为了简化,本例中客户端和服务器是在同一个工程中的,实际上需要用Eclipse把ICalculator接口导出成jar包在导入到客户端工程中即可,客户端代码保持不变。这里也可以使用jdk提供的wsimport命令导出服务端jar包,续。。。

webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)的更多相关文章

  1. 快速搭建Kerberos服务端及入门使用

    快速搭建Kerberos服务端及入门使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Kerberos是一种网络身份验证协议.它旨在通过使用秘密密钥加密为客户端/服务器应用程序提 ...

  2. webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端

    1.Define the interface import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebRe ...

  3. 10SpringMvc_springmvc快速入门小案例(注解版本)

    第一步:新建案例工程:

  4. 001-ant design安装及快速入门【基于纯antd的基本项目搭建】

    一.安装使用 1.1.安装 推荐使用 npm 或 yarn 的方式进行开发 npm install antd --save yarn add antd 1.2.浏览器引入 在浏览器中使用 script ...

  5. Maven快速入门(一)Maven介绍及环境搭建

    做开发的程序员都知道,在系统开发需要各自各样的框架.工具.其中有一种工具不管你是初级程序员还是高级程序员都必须熟练掌握的,那就是项目管理工具(maven.ant.gradle).接下来就总结Maven ...

  6. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  7. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

  8. WebService技术,服务端and客户端JDK-wsimport工具(一)

    使用webservice服务,需要了解几个名词:soap 简单对象协议.http+xml . WSDL 先看下代码结构: 服务端代码与客户端代码分别处于两不同的包中 一.服务端内容 服务端: @Web ...

  9. myeclipse-建立webservice服务端和客户端

    一.建立webservice服务端: 1.新建一个web service project,名称为webservice_server截图如下,点击finish. 2.选择工程,点击右键,选择new-&g ...

随机推荐

  1. (剑指Offer)面试题3:二维数组中的查找

    题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路: 鉴于数组的规律 ...

  2. MFC 小知识总结二

    1 UpdateData 不能及时更新控件内容 原因: UpdateData是通过数据交换实现改变控件显示的.改变了之后必需要调用一次OnPaint才干实现刷新. 因此,若是在一个函数里重复多次调用有 ...

  3. B - Sort the Array

    找出一个递减序列,假设有两个或两个以上递减序列直接no了,然后对递减序列两端数start.end,然后比較a[start]和a[end+1] . a[end] 和a[start-1] #include ...

  4. ZH奶酪:Ubuntu客户端通过SSH方式远程登录Ubuntu服务器

    1.概述 传统的远程登录(telnet,rlogin)时不安全的,他们在网络上用明文传输口令和数据,SSH则是安全的,openssh提供两种级别的验证方式. (1)基于口令的安全验证:知道服务器的帐号 ...

  5. alias的使用

    alias,即别名.可以通过较短的别名,来实现对应的真实的命令.将alias定义在.bash_profile文件中,即可在任意目录下执行自己定义的这些命令了. 1. 命令 alias myDir='c ...

  6. Websocket——Websocket原理

    偶然在知乎上看到一篇回帖,瞬间认为之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比較喜欢看这样的博客,读起来非常轻松.不枯燥,没有布道师的 ...

  7. oracle 多字段去重查询

      oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...

  8. 类的专有方法(__getitem__和__setitem__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__getitem__和__s ...

  9. C#多线程之 ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

  10. oracl查询锁表语句

    技能源于生活的不断实践,实践是对知识的不断扩展和总结.汇总.进而形成思想体系! --查询锁表语句 select sess.sid, sess.serial#, lo.oracle_username, ...