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服务有两种类型:
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 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 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 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服务器,并使它们可用于客户端应用程序。
Some of the advantages of web services are:
Web服务的一些优点是:
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的一部分,因此我们不需要包括其他任何与之兼容的东西。
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;
}
@Override
public 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 {
@WebMethod
public boolean addPerson(Person p);
@WebMethod
public boolean deletePerson(int id);
@WebMethod
public Person getPerson(int id);
@WebMethod
public 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>();
@Override
public boolean addPerson(Person p) {
if(persons.get(p.getId()) != null) return false;
persons.put(p.getId(), p);
return true;
}
@Override
public boolean deletePerson(int id) {
if(persons.get(id) == null) return false;
persons.remove(id);
return true;
}
@Override
public Person getPerson(int id) {
return persons.get(id);
}
@Override
public 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 name
QName qname = new QName("https://service.jaxws.journaldev.com/", "PersonServiceImplService");
Service service = Service.create(wsdlURL, qname);
//We need to pass interface and model beans to client
PersonService 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 person
System.out.println("Add Person Status="+ps.addPerson(p1));
System.out.println("Add Person Status="+ps.addPerson(p2));
//get person
System.out.println(ps.getPerson(1));
//get all persons
System.out.println(Arrays.asList(ps.getAllPersons()));
//delete person
System.out.println("Delete Person Status="+ps.deletePerson(2));
//get all persons
System.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,因为它已在第一次运行中添加。
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 person
System.out.println(service.getPerson(1));
//get all persons
System.out.println(Arrays.asList(service.getAllPersons()));
//delete person
System.out.println("Delete Person Status="+service.deletePerson(2));
//get all persons
System.out.println(Arrays.asList(service.getAllPersons()));
}
}
That’s all for a quick JAX-WS tutorial.
这就是快速JAX-WS教程的全部内容。
jax-ws使用教程