一、概述

  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

最新文章

  1. linux 查找文件和搜索文件
  2. HTML入门篇
  3. Beta阶段站立会议-02
  4. FR #1题解
  5. Android中的PopupWindow详解
  6. 部署K2 Blackpearl流程时出错(与基础事务管理器的通信失败或Communication with the underlying transaction manager has failed.
  7. 心情记录&amp;考试总结 3.30
  8. php生成excel或php生成csv
  9. 190. Reverse Bits
  10. JavaScript删除数组重复元素的5个高效算法
  11. C#基础知识—父类和子类的关系
  12. 再回首,Java温故知新——开篇说明
  13. JMeter数据库性能测试
  14. URL重写是实现PHP伪静态
  15. Linux网络管理——Linux网络命令
  16. 高德地图测两点距离android比较精确的
  17. 百度SMS SDK for .Net
  18. 再叙Java反射
  19. 仿 ELEMENTUI 实现一个简单的 Form 表单
  20. ARM的9种寻址方式

热门文章

  1. gpuimage的各种滤镜简介
  2. 实验楼 linux 学习
  3. 深入理解C++对象模型
  4. 网格弹簧质点系统模拟(Spring-Mass System by Fast Method)附源码
  5. Unity3D脚本行尾(Line Endings)
  6. Enum简单例子DropdownList
  7. How to create a launcher for a manually extracted program in Ubuntu
  8. Asp.Net MVC3 简单入门详解过滤器Filter(转)
  9. Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案(转)
  10. CSS基本知识6-CSS字体