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. 字段计算器VBS

    ArcGIS属性表中右键可调用字段计算器.写一些简单代码可操作属性表,有VBS和Python两种. 现在要求是:如果"地块编码"为空,则将"地块编号"赋给&qu ...

  2. 11g relocate scan ip

    今天安装了Oracle 11.2.0.4的数据库,由于在安装GRID软件是,跑脚本的时候是现在节点2上跑的,跑完之后然后在节点1上跑.发现我的scan_ip在节点2上,我想把scan_ip reloc ...

  3. vim配置 高亮+自动缩进+行号+折叠+优化

    一:修改 .vimrc即可 二: set nocompatible " 关闭 vi 兼容模式syntax on " 自动语法高亮colorscheme molokai " ...

  4. uni-app - Class 与 Style 绑定

    参考uni文档:https://uniapp.dcloud.io/use?id=class-%E4%B8%8E-style-%E7%BB%91%E5%AE%9A 参考vue文档:https://cn. ...

  5. Android 四大组件学习之BroadcastReceiver三

    本节学习广播的分类. 广播分为无序广播和有序广播 无序广播: 广播发送者的action与广播接收者的action都匹配的话,所以广播介绍者都能够收到这条广播,而且没有先后顺序,能够觉得是同一时候收到 ...

  6. 文本框只支持数字、小数点、退格符、负号、Del键

    Public Function OnlyNumberAndDot(inKeyAscii As Integer) As Integer '函数说明:文本框只支持数字.小数点.退格符.负号.Del键 '入 ...

  7. sqlserver2008r2安装

  8. HDU 1017 A Mathematical Curiosity (数学)

    A Mathematical Curiosity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. HTML+CSS浏览器兼容性问题

    浏览器兼容问题一:不同浏览器的标签默认的外补丁和内补丁不同 问题症状:随便写几个标签,不加样式控制的情况下,各自的margin 和padding差异较大. 碰到频率:100% 解决方案:CSS里    ...

  10. WordPress 主题教程:从零开始制作 WordPress 主题

    为什么要开发WordPress主题? WordPress主题由一系列文件和样式表单组成,这些文件和样式表单共同作用生成WordPress网站的外观.每个主题都不同,用户可以通过这些主题随心所欲地更换自 ...