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. VS2008+Windows DDK 7的环境配置

    Mark offers some third party utilities. That's good, but I will show a more handy way (IMHO): how to ...

  2. Discuz 学习笔记一 :getgdc 和get_client_ip

    Getgdc函数 discuz有一个超级变量的自定义函数:   function getgpc($k, $type='GP') {     $type = strtoupper($type);     ...

  3. Could not find com.android.tools.build:gradle:3.0.0-alpha1 in circle ci

      Error:(1, 0) The android gradle plugin version 3.0.0-alpha1 is too old, please update to the lates ...

  4. (算法)两个有序数组的第k大的数

    题目: 有两个数组A和B,假设A和B已经有序(从大到小),求A和B数组中所有数的第K大. 思路: 1.如果k为2的次幂,且A,B 的大小都大于k,那么 考虑A的前k/2个数和B的前k/2个数, 如果A ...

  5. 漫话Asp.net

    经过一段时间的接触,对asp.net这一块进行了很多其它的了解,漫话一下. Asp.net与Web : asp.net属于动态网页技术,属于web应用程序开发. Web应用程序通常是B/S模式. 和B ...

  6. Python编程-基础知识-List

    Negative Indexes(负索引) >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[-1] 'e ...

  7. ifconf和ifreq

    http://blog.csdn.net/jasenwan88/article/details/7763689 用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人 ...

  8. 11g relocate scan ip

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

  9. adb(Android Debug Bridge)(一)

    上一篇介绍的am,pm命令都是基于adb shell下的命令.这节来详细介绍下adb命令. Android Debug Bridge(adb)是一个让你跟模拟器或者android设备通信的多功能命令. ...

  10. 通过Js对电话和姓名身份证等进行部分隐藏处理

    在进行web前端页面开发中,有时需要从后台获取用户数据来显示在前台页面,但是考虑到用户信息安全的问题,就需要对这些信息进行处理,使其不完全显示出来,例如姓名,两个字的显示姓,名字用*代替,电话前三位和 ...