jax-ws使用教程

Welcome to JAX-WS Tutorial. Web Services work on client-server model where they communicate over the network. Server side component provides the endpoint URL where service is located and client application can invoke different methods.

欢迎使用JAX-WS教程。 Web服务在客户端-服务器模型上工作,它们通过网络进行通信。 服务器端组件提供服务所在的端点URL,客户端应用程序可以调用不同的方法。

There are two types of web services:

Web服务有两种类型:

  1. SOAP Web ServicesSOAP Web服务
  2. Restful Web Services宁静的Web服务

JAX-WS教程 (JAX-WS Tutorial)

In this JAX-WS tutorial, we will use JAX-WS to create SOAP based web services. But first we will go through some of the jargon words used in SOAP web services.

在本JAX-WS教程中,我们将使用JAX-WS创建基于SOAP的Web服务。 但是首先,我们将介绍SOAP Web服务中使用的一些专业术语。

肥皂 (SOAP)

SOAP stands for Simple Object Access Protocol. SOAP is an XML based industry standard protocol for designing and developing web services. Since it’s XML based, it’s platform and language independent. So our server can be based on JAVA and client can be on .NET, PHP etc. and vice versa.

SOAP代表简单对象访问协议。 SOAP是用于设计和开发Web服务的基于XML的行业标准协议。 由于它基于XML,因此与平台和语言无关。 因此,我们的服务器可以基于JAVA,客户端可以基于.NET,PHP等,反之亦然。

WSDL (WSDL)

WSDL stands for Web Service Description Language. WSDL is an XML based document that provides technical details about the web service. Some of the useful information in WSDL document are: method name, port types, service end point, binding, method parameters etc.

WSDL代表Web服务描述语言。 WSDL是基于XML的文档,提供有关Web服务的技术详细信息。 WSDL文档中的一些有用信息包括:方法名称,端口类型,服务端点,绑定,方法参数等。

UDDI (UDDI)

UDDI is acronym for Universal Description, Discovery and Integration. UDDI is a directory of web services where client applications can lookup for web services. Web Services can register to the UDDI server and make them available to client applications.

UDDI是通用描述,发现和集成的缩写。 UDDI是Web服务的目录,客户端应用程序可以在其中查找Web服务。 Web服务可以注册到UDDI服务器,并使它们可用于客户端应用程序。

Web服务的优势 (Advantages of Web Services)

Some of the advantages of web services are:

Web服务的一些优点是:

  • Interoperability: Because web services work over network and use XML technology to communicate, it can be developed in any programming language supporting web services development.互操作性:因为Web服务在网络上工作并且使用XML技术进行通信,所以可以用支持Web服务开发的任何编程语言来开发它。
  • Reusability: One web service can be used by many client applications at the same time. For example, we can expose a web service for technical analysis of a stock and it can be used by all the banks and financial institutions.可重用性:一个Web服务可以同时被许多客户端应用程序使用。 例如,我们可以公开用于股票技术分析的Web服务,并且所有银行和金融机构都可以使用它。
  • Loose Coupling: Web services client code is totally independent with server code, so we have achieved loose coupling in our application. This leads to easy maintenance and easy to extend.松散耦合:Web服务客户端代码与服务器代码完全独立,因此我们在应用程序中实现了松散耦合。 这导致易于维护并且易于扩展。
  • Easy to deploy and integrate易于部署和集成
  • Multiple service versions can be running at same time.可以同时运行多个服务版本。

JAX-WS (JAX-WS)

JAX-WS stands for Java API for XML Web Services. JAX-WS is XML based Java API to build web services server and client application. It’s part of standard Java API, so we don’t need to include anything else which working with it.

JAX-WS代表XML Web Services的Java API。 JAX-WS是基于XML的Java API,用于构建Web服务服务器和客户端应用程序。 它是标准Java API的一部分,因此我们不需要包括其他任何与之兼容的东西。

JAX-WS示例 (JAX-WS Example)

Now that we have gone through the web services terminologies, let’s go ahead and create a JAX-WS web service. We will create a web service that will expose methods to add, delete and get person objects. So first of all we will create a model bean for our data.

现在我们已经遍历了Web服务术语,让我们继续创建一个JAX-WS Web服务。 我们将创建一个Web服务,该服务将公开添加,删除和获取人员对象的方法。 因此,首先,我们将为数据创建一个模型bean。

Person.java

Person.java

package com.journaldev.jaxws.beans;import java.io.Serializable;public class Person implements Serializable{private static final long serialVersionUID = -5577579081118070434L;private String name;private int age;private int id;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString(){return id+"::"+name+"::"+age;}}

Now we will have to create an interface where we will declare the methods we will expose in our JAX-WS example web services.

现在,我们将不得不创建一个接口,在该接口中声明将在我们的JAX-WS示例Web服务中公开的方法。

PersonService.java

PersonService.java

package com.journaldev.jaxws.service;import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;import com.journaldev.jaxws.beans.Person;@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PersonService {@WebMethodpublic boolean addPerson(Person p);@WebMethodpublic boolean deletePerson(int id);@WebMethodpublic Person getPerson(int id);@WebMethodpublic Person[] getAllPersons();
}

Notice the use of @WebService and @SOAPBinding annotations from JAX-WS API. We can create SOAP web services in RPC style or Document style. We can use any of these styles to create web services, the different is seen in the way WSDL file is generated.

请注意,JAX-WS API使用了@WebService@SOAPBinding批注。 我们可以以RPC样式Document样式创建SOAP Web服务。 我们可以使用这些样式中的任何一种来创建Web服务,不同之处在于WSDL文件的生成方式。

Now we will write the implementation class as shown below.

现在,我们将编写实现类,如下所示。

PersonServiceImpl.java

PersonServiceImpl.java

package com.journaldev.jaxws.service;import java.util.HashMap;
import java.util.Map;
import java.util.Set;import javax.jws.WebService;import com.journaldev.jaxws.beans.Person;@WebService(endpointInterface = "com.journaldev.jaxws.service.PersonService")
public class PersonServiceImpl implements PersonService {private static Map<Integer,Person> persons = new HashMap<Integer,Person>();@Overridepublic boolean addPerson(Person p) {if(persons.get(p.getId()) != null) return false;persons.put(p.getId(), p);return true;}@Overridepublic boolean deletePerson(int id) {if(persons.get(id) == null) return false;persons.remove(id);return true;}@Overridepublic Person getPerson(int id) {return persons.get(id);}@Overridepublic Person[] getAllPersons() {Set<Integer> ids = persons.keySet();Person[] p = new Person[ids.size()];int i=0;for(Integer id : ids){p[i] = persons.get(id);i++;}return p;}}

Most important part is the @WebService annotation where we are providing endpointInterface value as the interface we have for our web service. This way JAX-WS know the class to use for implementation when web service methods are invoked.

最重要的部分是@WebService批注,我们在其中提供endpointInterface值作为Web服务的接口。 这样,当Web服务方法被调用时,JAX-WS知道要用于实现的类。

Our web service business logic is ready, let’s go ahead and publish it using JAX-WS Endpoint class.

我们的Web服务业务逻辑已经准备就绪,让我们继续使用JAX-WS Endpoint类进行发布。

SOAPPublisher.java

SOAPPublisher.java

package com.journaldev.jaxws.service;import javax.xml.ws.Endpoint;public class SOAPPublisher {public static void main(String[] args) {Endpoint.publish("https://localhost:8888/ws/person", new PersonServiceImpl());  }}

Just run the above program and your web service will be published at the given endpoint in the program. We can access it’s WSDL document by adding ?wsdl to the endpoint url as shown in below image.

只需运行以上程序,您的Web服务就会在程序中的给定端点上发布。 我们可以通过将?wsdl添加到端点url来访问它的WSDL文档,如下图所示。

Here is the WSDL code, we will use some of the values from these while writing the client code.

这是WSDL代码,我们在编写客户端代码时将使用其中的一些值。

person.wsdl

person.wsdl

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (https://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><!-- Generated by JAX-WS RI (https://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><definitions xmlns:wsu="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="https://www.w3.org/ns/ws-policy" xmlns:wsp1_2="https://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="https://www.w3.org/2007/05/addressing/metadata" xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="https://service.jaxws.journaldev.com/" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns="https://schemas.xmlsoap.org/wsdl/" targetNamespace="https://service.jaxws.journaldev.com/" name="PersonServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="https://service.jaxws.journaldev.com/" schemaLocation="https://localhost:8888/ws/person?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="addPerson">
<part name="arg0" type="tns:person"></part>
</message>
<message name="addPersonResponse">
<part name="return" type="xsd:boolean"></part>
</message>
<message name="deletePerson">
<part name="arg0" type="xsd:int"></part>
</message>
<message name="deletePersonResponse">
<part name="return" type="xsd:boolean"></part>
</message>
<message name="getPerson">
<part name="arg0" type="xsd:int"></part>
</message>
<message name="getPersonResponse">
<part name="return" type="tns:person"></part>
</message>
<message name="getAllPersons"></message>
<message name="getAllPersonsResponse">
<part name="return" type="tns:personArray"></part>
</message>
<portType name="PersonService">
<operation name="addPerson">
<input wsam:Action="https://service.jaxws.journaldev.com/PersonService/addPersonRequest" message="tns:addPerson"></input>
<output wsam:Action="https://service.jaxws.journaldev.com/PersonService/addPersonResponse" message="tns:addPersonResponse"></output>
</operation>
<operation name="deletePerson">
<input wsam:Action="https://service.jaxws.journaldev.com/PersonService/deletePersonRequest" message="tns:deletePerson"></input>
<output wsam:Action="https://service.jaxws.journaldev.com/PersonService/deletePersonResponse" message="tns:deletePersonResponse"></output>
</operation>
<operation name="getPerson">
<input wsam:Action="https://service.jaxws.journaldev.com/PersonService/getPersonRequest" message="tns:getPerson"></input>
<output wsam:Action="https://service.jaxws.journaldev.com/PersonService/getPersonResponse" message="tns:getPersonResponse"></output>
</operation>
<operation name="getAllPersons">
<input wsam:Action="https://service.jaxws.journaldev.com/PersonService/getAllPersonsRequest" message="tns:getAllPersons"></input>
<output wsam:Action="https://service.jaxws.journaldev.com/PersonService/getAllPersonsResponse" message="tns:getAllPersonsResponse"></output>
</operation>
</portType>
<binding name="PersonServiceImplPortBinding" type="tns:PersonService">
<soap:binding transport="https://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
<operation name="addPerson">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</output>
</operation>
<operation name="deletePerson">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</output>
</operation>
<operation name="getPerson">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</output>
</operation>
<operation name="getAllPersons">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="https://service.jaxws.journaldev.com/"></soap:body>
</output>
</operation>
</binding>
<service name="PersonServiceImplService">
<port name="PersonServiceImplPort" binding="tns:PersonServiceImplPortBinding">
<soap:address location="https://localhost:8888/ws/person"></soap:address>
</port>
</service>
</definitions>

Here is a client program where we are invoking our JAX-WS example web service.

这是一个客户端程序,我们在其中调用我们的JAX-WS示例Web服务。

SOAPPublisherClient.java

SOAPPublisherClient.java

package com.journaldev.jaxws.service;import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;import javax.xml.namespace.QName;
import javax.xml.ws.Service;import com.journaldev.jaxws.beans.Person;public class SOAPPublisherClient {public static void main(String[] args) throws MalformedURLException {URL wsdlURL = new URL("https://localhost:8888/ws/person?wsdl");//check above URL in browser, you should see WSDL file//creating QName using targetNamespace and nameQName qname = new QName("https://service.jaxws.journaldev.com/", "PersonServiceImplService"); Service service = Service.create(wsdlURL, qname);  //We need to pass interface and model beans to clientPersonService ps = service.getPort(PersonService.class);Person p1 = new Person(); p1.setName("Pankaj"); p1.setId(1); p1.setAge(30);Person p2 = new Person(); p2.setName("Meghna"); p2.setId(2); p2.setAge(25);//add personSystem.out.println("Add Person Status="+ps.addPerson(p1));System.out.println("Add Person Status="+ps.addPerson(p2));//get personSystem.out.println(ps.getPerson(1));//get all personsSystem.out.println(Arrays.asList(ps.getAllPersons()));//delete personSystem.out.println("Delete Person Status="+ps.deletePerson(2));//get all personsSystem.out.println(Arrays.asList(ps.getAllPersons()));}}

When we execute above JAX-WS client program, we get this output.

当我们执行上面的JAX-WS客户端程序时,我们得到此输出。

Add Person Status=true
Add Person Status=true
1::Pankaj::30
[1::Pankaj::30, 2::Meghna::25]
Delete Person Status=true
[1::Pankaj::30]

When I run the program again, we get this output.

当我再次运行程序时,我们得到此输出。

Add Person Status=false
Add Person Status=true
1::Pankaj::30
[1::Pankaj::30, 2::Meghna::25]
Delete Person Status=true
[1::Pankaj::30]

Notice that in the second run, add person status is false because it was already added in the first run.

请注意,在第二次运行中,添加人员状态为false,因为它已在第一次运行中添加。

JAX-WS客户端程序 (JAX-WS Client Program)

If you look at the above program, we are using the server code itself. However web services just expose WSDL and third party applications don’t have access to these classes. So in that case, we can use wsimport utility to generate the client stubs. This utility comes with standard installation of JDK. Below image shows what all java classes we get when we run this utility.

如果您看上面的程序,我们正在使用服务器代码本身。 但是,Web服务仅公开WSDL,并且第三方应用程序无权访问这些类。 因此,在那种情况下,我们可以使用wsimport实用程序生成客户端存根。 该实用程序随JDK的标准安装一起提供。 下图显示了运行此实用程序时得到的所有Java类。

Just copy these classes into your client project, the only change would be the way we get PersonService instance.

只需将这些类复制到您的客户端项目中,唯一的变化就是我们获取PersonService实例的方式。

Below is the program for this, the output will be same as above client program. Notice the use of PersonServiceImplService class and it’s method getPersonServiceImplPort to get the PersonService instance.

下面是用于此的程序,输出将与上面的客户端程序相同。 请注意,使用PersonServiceImplService类以及使用getPersonServiceImplPort方法获取PersonService实例。

TestPersonService.java

TestPersonService.java

package com.journaldev.jaxws.service.test;import java.util.Arrays;import com.journaldev.jaxws.service.Person;
import com.journaldev.jaxws.service.PersonService;
import com.journaldev.jaxws.service.PersonServiceImplService;public class TestPersonService {public static void main(String[] args) {PersonServiceImplService serviceImpl = new PersonServiceImplService();PersonService service = serviceImpl.getPersonServiceImplPort();Person p1 = new Person(); p1.setName("Pankaj"); p1.setId(1); p1.setAge(30);Person p2 = new Person(); p2.setName("Meghna"); p2.setId(2); p2.setAge(25);System.out.println("Add Person Status="+service.addPerson(p1));System.out.println("Add Person Status="+service.addPerson(p2));//get personSystem.out.println(service.getPerson(1));//get all personsSystem.out.println(Arrays.asList(service.getAllPersons()));//delete personSystem.out.println("Delete Person Status="+service.deletePerson(2));//get all personsSystem.out.println(Arrays.asList(service.getAllPersons()));}}

That’s all for a quick JAX-WS tutorial.

这就是快速JAX-WS教程的全部内容。

翻译自: https://www.journaldev.com/9123/jax-ws-tutorial

jax-ws使用教程

jax-ws使用教程_JAX-WS教程相关推荐

  1. 系统开发系列 之MyEclipse创建WebService详细教程和调用教程(spring框架+maven+CXF框架)

    1 回顾 [系统开发系列 之MyEclipse创建WebService详细教程和调用教程]介绍了使用JWS实现WebService接口的发布和调用,主要涉及的点有: (1)MyEclipse点击Fil ...

  2. php更改asp.net教程,ASP.NET 教程

    ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式: Web Pages(Web 页面).MVC(Mod ...

  3. serv-u 自定义html,Serv-U架设教程_Serv-U使用教程图文版

    Serv-U是一款很好用的FTP服务器软件,本文就给大家详细介绍一下<Serv-U架设教>,希望对广大新手有用. Serv-U架设教程_Serv-U使用教程图文版: 1.到文末下载 Ser ...

  4. UML基础教程(内部使用教程) 非常不错的ppt!!强烈推荐

    UML基础教程(内部使用教程) 非常不错的ppt http://wenku.baidu.com/view/66de89d63186bceb19e8bb1e.html 大家可以参考下 目录 1. 前言 ...

  5. C#游戏开发快速入门教程Unity5.5教程

    C#游戏开发快速入门教程Unity5.5教程 试读文档下载地址:http://pan.baidu.com/s/1slwBHoD C#是微软发布的高级程序设计语言,这门语言和C语言一样,已经成为了大学计 ...

  6. python语言教程-Python 基础教程

    Python基础教程 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Pe ...

  7. python菜鸟教程h-python菜鸟教程,python好玩又简单的代码

    如果是零基础的话推荐你看以下几本书,入门来说都还不错:"笨办法"学Python(第3版)HeadFirstPython(中文版)父与子的编程之旅:与小卡特一起学Python pyt ...

  8. TensorFlow教程之完整教程 2.7 字词的向量表示

     TensorFlow教程之完整教程 2.7 字词的向量表示 知与谁同 2017-08-22 15:37:40 浏览67 评论0 函数 摘要: 本文档为TensorFlow参考文档,本转载已得到T ...

  9. mysql5 7安装教程_MySQL57安装教程

    MySQL57安装教程... --------------------------- 首先需要下载MySQL57安装包: --------------------------------------- ...

  10. java反射教程_Java反射教程

    java反射教程 在本教程中,我主要编写一些示例来介绍Java反射可以做什么. 希望它可以给您这个概念的概述. 请留下您的评论以寻求建议. 什么是反射? 简而言之,反射是程序在运行时检查和修改对象的结 ...

最新文章

  1. 2022-2028年中国硝化棉行业市场研究及前瞻分析报告
  2. 卷积输出的记录,为什么是([3, 0, 1, 2])
  3. (七) shiro 加密与解密
  4. 深入理解JavaScript中的this关键字
  5. UDID被禁用后的集中替代品
  6. MTK 10A常用函数集锦
  7. android4.0 底部菜单,Android自定义控件系列(四)—底部菜单(下)
  8. 怎么判断网络回路_地暖管漏水怎么办?一打、二看、三确定,及时查出地暖管漏水点!...
  9. 设置SUID用于提权或降权
  10. 数据结构------图-----深度广度优先遍历
  11. idea抽取重复方法快捷键_idea 常用快捷键
  12. 苹果系统macos腾讯企点无法打开麦克风权限
  13. 狗都能看懂的CenterNet讲解及代码复现
  14. python快手爬虫:解决粉丝数、 关注数等字体加密
  15. python绘制樱花洒落_Python:绘制樱花树
  16. matlab---s函数讲解之二连杆动力学仿真
  17. 途志分享几个抖音短视频拍摄技巧
  18. centos7网卡问题
  19. 一款客服系统有哪些必备的功能模块?
  20. Laravel 的 Auth::attempt () 初探及修改 bcrypt 验证为 MD5

热门文章

  1. Shell-杀死指定进程
  2. 谷歌技术quot;三宝quot;之MapReduce
  3. QTcpSocket 发送数据的几种方法
  4. 80后营销人如何为理想插上丰满“羽翼”?
  5. 语音信号处理基础(五)——语音分帧与加窗
  6. [转载] numpy.minimum
  7. [转载] Java异常:选择Checked Exception还是Unchecked Exception?
  8. UDP协议和socketserver以及文件上传
  9. 【linux之bash】
  10. Tomcat报错:ERROR:transport error 202: gethostbyname: unknown host