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 ...
随机推荐
- mybatis结果的组装(springboot)
文主要解答一个问题,即如果bean没有setter,而且属性不是public的,mybatis的自动组装是否可以赋值成功的问题. 查询调用过程 DefaultSqlSession.selectList ...
- easyui combobox实现本地模糊查询
直接上代码 $("#combobox1").combobox({ valueField : "value", textField : "text&qu ...
- linux下线刷硬盘
Linux系统往往有添加磁盘不够的情况,这时就需要添加新的硬盘.一般情况下需要重启服务器,这里我们来使用线刷方式读取Linux新增硬盘 1.添加磁盘后fdisk -l磁盘没有显示 2.查看主机总线号 ...
- POJ--2449--Remmarguts' Date【dijkstra_heap+A*】第K短路
链接:http://poj.org/problem?id=2449 题意:告诉你有n个顶点,m条边.并把这些边的信息告诉你:起点.终点.权值.再告诉你s.t.k.需求出s到t的第k短路,没有则输出-1 ...
- Linux c 管道文件-进程间的通信 mkfifo、pipe
管道文件: 1. 创建管道mkfifo(命名管道) #include<sys/stat.h> int mkfifo( const char *pathname, mode_ ...
- C和C++静态检查规范
- Java中的软(弱)引用
一.Java中的强.软.弱.虚引用 在JDK中我们能够看到有一个java.lang.ref的包.这个包中就是Java中实现强.软.弱.虚引用的包,例如以下: PhantomReference 虚引用: ...
- 解决 jquery.form.js和springMVC上传 MultipartFile取不到信息
前段页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- exception 值太大
CreateTime--2018年2月5日09:45:01 Author:Marydon 异常: Cause: java.sql.SQLException: ORA-12899: 列 " ...
- glob 模块
# -*- coding: utf-8 -*- #python 27 #xiaodeng #glob 模块 #http://python.jobbole.com/81552/ #查找文件只用到三个匹配 ...