webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
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服务端和客户端(一)的更多相关文章
- 快速搭建Kerberos服务端及入门使用
快速搭建Kerberos服务端及入门使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Kerberos是一种网络身份验证协议.它旨在通过使用秘密密钥加密为客户端/服务器应用程序提 ...
- webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端
1.Define the interface import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebRe ...
- 10SpringMvc_springmvc快速入门小案例(注解版本)
第一步:新建案例工程:
- 001-ant design安装及快速入门【基于纯antd的基本项目搭建】
一.安装使用 1.1.安装 推荐使用 npm 或 yarn 的方式进行开发 npm install antd --save yarn add antd 1.2.浏览器引入 在浏览器中使用 script ...
- Maven快速入门(一)Maven介绍及环境搭建
做开发的程序员都知道,在系统开发需要各自各样的框架.工具.其中有一种工具不管你是初级程序员还是高级程序员都必须熟练掌握的,那就是项目管理工具(maven.ant.gradle).接下来就总结Maven ...
- 使用Spring框架入门四:基于注解的方式的AOP的使用
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
- webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成
首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...
- WebService技术,服务端and客户端JDK-wsimport工具(一)
使用webservice服务,需要了解几个名词:soap 简单对象协议.http+xml . WSDL 先看下代码结构: 服务端代码与客户端代码分别处于两不同的包中 一.服务端内容 服务端: @Web ...
- myeclipse-建立webservice服务端和客户端
一.建立webservice服务端: 1.新建一个web service project,名称为webservice_server截图如下,点击finish. 2.选择工程,点击右键,选择new-&g ...
随机推荐
- [Node.js]23. Level 4: Semantic versioning
Update the versions on your dependencies to be a little more flexible, adding the ~ in front of your ...
- Java 抽象类和接口有什么差别
抽象类和接口有什么差别? 1. 抽象类在java语言中所表示的是一种继承关系,一个子类仅仅能继承一个父类.可是能够实现多个接口. 2. 在抽象类中能够拥有自己的成员变量和非抽象类方法,可是接口中仅仅能 ...
- C#.NET常见问题(FAQ)-listView如何显示网格线
把GridLines设置为True 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: http:/ ...
- VM虚拟机安装之后出现无法自动登录到桌面以及__vmware_user__怎么办
1 运行control userpasswords2 打开用户账户对话框,点击高级选项中的"高级"按钮 2 右击" __vmware_user__"这个账户,选 ...
- 解决pl/sql 查询数据中文显示成?
解决方法: 1.打开 PLSQL Developer 安装目录下,看到有PLSQLDev.exe的目录, 在PLSQL Developer文件夹内新建“PLSql_run.bat”文件,在该文件中输入 ...
- jdbc详解(一)
JDBC简介 l 数据库驱动 SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC l JDBC 全称为: Java Data Base Connectivity ...
- poj 1879 Truck History
本题链接:点击打开链接 题目大意: 输入n表示卡车辆数,输入每辆卡车编号.即长度为7的字符串,每辆卡车编号均可由其他类型编号衍生过来,求由当中一辆衍生出其他全部的最小衍生次数(有一个字符不同就需衍生一 ...
- 源码安装和配置zabbix 3.0 LST
Zabbix是什么 Zabbix 是由Alexei Vladishev创建,目前由Zabbix SIA在持续开发和支持. Zabbix 是一个企业级的分布式开源监控方案. Zabbix是一款能够监控各 ...
- iOS-APP启动页加载广告
概述 加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新. 详细 代码下载:http://www.demodashi.com/demo/10698.html 目前市场上很多APP(如淘 ...
- Integer 内部实现
public static void main(String[] args) { Integer in1 = 128; Integer in2 = 128; System.out.println(in ...