一、概述

  Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API。

  在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。

  在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。

二、使用jdk的JAX-WS发布服务

1.写服务端的接口

 package com.webservice.jaxws;

 public interface Hello {
public String sayHello(String name);
}

2.写服务端的实现(使用注解@WebService)这个必须在实现类写,不然报错(class com.webservice.jaxws.HelloImpl has neither @WebService nor @WebServiceProvider annotation)

 package com.webservice.jaxws;

 import javax.jws.WebService;
@WebService
public class HelloImpl implements Hello{ @Override
public String sayHello(String name) {
return "hi, "+name;
} }

3.使用jdk中Endpoint发布服务

package com.webservice.jaxws;

import javax.xml.ws.Endpoint;

public class HelloServerPub {
public static void main(String[] args) {
Hello hello = new HelloImpl();
Endpoint.publish("http://localhost:8080/hello", hello);
System.out.println("发布成功!");
}
}

4.在浏览器地址栏输入服务发布的地址查看wsdl及schema文件

 <?xml version="1.0" encoding="UTF-8"?><!-- 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://jaxws.webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.webservice.com/" name="HelloImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://jaxws.webservice.com/" schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloImpl">
<operation name="sayHello">
<input wsam:Action="http://jaxws.webservice.com/HelloImpl/sayHelloRequest" message="tns:sayHello"></input>
<output wsam:Action="http://jaxws.webservice.com/HelloImpl/sayHelloResponse" message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloImplPortBinding" type="tns:HelloImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloImplService">
<port name="HelloImplPort" binding="tns:HelloImplPortBinding">
<soap:address location="http://localhost:8080/hello"></soap:address>
</port>
</service>
</definitions>
 <?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><xs:schema xmlns:tns="http://jaxws.webservice.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://jaxws.webservice.com/">

 <xs:element name="sayHello" type="tns:sayHello"></xs:element>

 <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"></xs:element>

 <xs:complexType name="sayHello">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"></xs:element>
</xs:sequence>
</xs:complexType> <xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

三、客户端

1.根据wsdl生成客户端(打开命令行窗口,切换到src目录,执行"wsimport -keep http://localhost:8080/hello?wsdl"生成客户端代码,如下图所示:)

2、 借助生成的代码编写调用WebService对外提供的方法

  wsimport工具帮我们生成了好几个java类,但我们只需要关心HelloImplService类和HelloImpl接口的使用即可

 package com.webservice;

 import com.webservice.jaxws.HelloImpl;
import com.webservice.jaxws.HelloImplService; public class ClientTest {
public static void main(String[] args) {
//创建一个用于产生HelloImpl实例的工厂,HelloImplService类是wsimport工具生成的
HelloImplService service = new HelloImplService();
//通过工厂生成HelloImpl一个实例
HelloImpl hello = service.getHelloImplPort();
//调用HelloImpl接口的方法
String value = hello.sayHello("Tom");
System.out.println(value);
}
}

打印的结果:hi, Tom

JAX-WS服务端及客户端的更多相关文章

  1. webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)

    1.定义接口 package org.WebService.ws.annotation; import javax.jws.WebService; @WebService public interfa ...

  2. WebService-03-使用CXF开发服务端和客户端

    写在前面的话 前面两节说了使用Java提供的包开发服务端和客户端,现在使用CXF来开发,CXF提供了两个类发而服务,一个是ServerFactoryBean,另一个是JaxWsServerFactor ...

  3. WebSocket——SuperWebSocket实现服务端和客户端

    WebSocket——SuperWebSocket实现服务端和客户端具体实现如下: 注:本作者是基于vs2019 enterprise版本,所有项目均为.Net Framwork4.7版本(因为Web ...

  4. 使用WebSocket实现服务端和客户端的通信

    开发中经常会有这样的使用场景.如某个用户在一个数据上做了xx操作, 与该数据相关的用户在线上的话,需要实时接收到一条信息. 这种可以使用WebSocket来实现. 另外,对于消息,可以定义一个类进行固 ...

  5. asp.net获取服务端和客户端信息

    asp.net获取服务端和客户端信息 获取服务器名:Page.Server.ManchineName获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNam ...

  6. python thrift 服务端与客户端使用

    一.简介 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, ...

  7. IE8下服务端获取客户端文件的路径为C:/fakePath问题的解决方案

    上一篇文章上提到,IE8下服务端获取客户端文件的路径时,会变成C:/fakePath问题,于是乎通过文件路径去获得文件大小就失败了. 上网搜了一下,主要原因是IE8因为安全考虑,在上传文件时屏蔽了真实 ...

  8. 如何排查APP服务端和客户端是否支持ATS

    服务端排查 取得客户端直接连接的服务端域名及端口,例如mob.com.cn,端口443,即HTTPS默认端口.针对公网可访问的生产环境地址,建议使用的在线监测工具.https://wosign.ssl ...

  9. (转)SVN 服务端、客户端安装及配置、导入导出项目

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  10. linux(centos 6.4)下安装php memcache服务端及其客户端(详细教程)

    前言 在搭建个人博客时,由于没有使用任何框架,纯手工code前台和后台,导致遇到许多问题,其中一个问题就是mysql连接导致的页面相应速度异常低.在查询各种途径后,只能考虑使用memcache缓存.在 ...

随机推荐

  1. Spring中常用类型的bean配置(Map,List,Set,基本类型)

    给自己做个笔记... 有时会用到配置文件中配置一下映射关系,方便以后扩展.此时可采用集合类型的bean配置方式配置.程序中直接注入即可. map类型的: <!-- 旧版方式,无需util包 -- ...

  2. 数据处理之PostgreSQL过程语言学习

    前段时间,公司更换新的PostgreSQL数据集市的系统过程中,自己下载了postgresqlAPI的pdf文件研究了一下PostgreSQL数据集市.发现使用PostgreSQL过程语言可以大大加快 ...

  3. 报表软件JS开发引用HTML DOM的location和document对象

    上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...

  4. struts2案例

    Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点.Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架下开发的程序提供很好的兼容性.下 ...

  5. CF732D. Exams[二分答案 贪心]

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. [No00006D]下载离线版的github for windows【以Github for Windows 3.0.110.为例】

    目录 先上地址后讲原理: 原理: 11个目录的文件怎么一口气下载呢? 最后,把下好的文件批量名,同时将GitHub.exe.manifest也放到软件根目录下(与GitHub.exe同级): 今后的猜 ...

  7. 大三上 —— IOS五天实训

    第二天: 注册使用xib:1.首先为xib文件创建对象--let nib = UINib(nibName: "xib文件名", bundle: nil).2.具体的控件注册该xib ...

  8. pcl曲面重建模块-贪婪三角形投影算法实例

    贪婪三角形投影算法 在pcl-1.8测试 #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include &l ...

  9. 初学C#和MVC的一些心得,弯路,总结,还有教训(4)--Cache 关于创建多个缓存实例

    asp.net中的数据缓存可以用 HttpRuntime.Cache ,这个是大家都知道的,但如果缓存的数据比较多,又比较杂乱,想要把缓存分开管理(也就是创建多个缓存实例)应该怎么做呢... 于是常规 ...

  10. jsoup开发网页客户端3

    这个系列好久没更新,最近好忙,老大说未来是Html5的,所以最近一直学习前端以及Html5的一些东西.Android5.0的诞生,让我们眼前一亮,独特的Material风格更是吸引了无数人. 话说不学 ...