博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Eclipse的Java示例中的SOAP Web服务
阅读量:2531 次
发布时间:2019-05-11

本文共 15290 字,大约阅读时间需要 50 分钟。

Soap Webservices in java can be developed in may ways. We learned about in our last tutorial, today we will learn how we can create SOAP web service and it’s client program using Eclipse. Here we will not use JAX-WS, we will be using Apache Axis that is integrated in the Eclipse and provide quick and easy way to transform a application into Java Web Service and create client stubs with test JSP page for testing purpose.

Java中的Soap Webservices可以通过多种方式开发。 我们在上一教程中了解了 ,今天我们将学习如何使用Eclipse创建SOAP Web服务及其客户端程序。 在这里,我们将不使用JAX-WS,而是将使用集成在Eclipse中的Apache Axis ,它提供了快速简便的方法来将应用程序转换为Java Web Service并创建带有测试JSP页面的客户端存根以进行测试。

Java中的SOAP Web服务 (SOAP Webservices in Java)

I am using Eclipse Mars Release (4.5.0) for this tutorial but I think these steps will work with older versions of eclipse too. Also make sure you have added Apache Tomcat or any other servlet container as server in the Eclipse. Let’s start with our Eclipse Web Service implementation now.

我在本教程中使用的是Eclipse Mars Release(4.5.0) ,但我认为这些步骤也适用于旧版本的eclipse。 还要确保已将Apache Tomcat或任何其他servlet容器添加为Eclipse中的服务器。 现在让我们从Eclipse Web Service实现开始。

SOAP Web服务示例 (SOAP Web Service Example)

Let’s get started with our SOAP web service example in Eclipse. First of all we will create a simple Dynamic Web Project in Eclipse that will contain the business logic for our application.

让我们开始使用Eclipse中的SOAP Web服务示例。 首先,我们将在Eclipse中创建一个简单的Dynamic Web Project ,其中将包含应用程序的业务逻辑。

Click on Next button above and you will get next page to provide your web project name and Target Runtime. Notice that I am using Apache Tomcat 8, you can use any other standard servlet container too.

单击上方的“下一步”按钮,您将获得提供您的Web项目名称和“ 目标运行时”的下一页。 注意,我正在使用Apache Tomcat 8,您也可以使用任何其他标准servlet容器。

Click on Next and you will be asked to provide “Context Root” and Content Directory location. You can leave them as default.

单击下一步,将要求您提供“上下文根”和内容目录位置。 您可以将它们保留为默认值。

Click on Finish and Eclipse will create the project skeleton for you. Let’s get started with our business logic. So for our example, we would like to publish a web service that can be used to add/delete/get an object. So first step is to create a model bean.

单击Finish,Eclipse将为您创建项目框架。 让我们开始我们的业务逻辑。 因此,对于我们的示例,我们想发布一个可用于添加/删除/获取对象的Web服务。 因此,第一步是创建模型bean。

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;	}}

Notice that above is a simple java bean, we are implementing Serializable interface because we will be transporting it over the network. We have also provided toString method implementation that will be used when we will print this object at client side.

注意,上面是一个简单的Java bean,我们正在实现Serializable接口,因为我们将通过网络传输它。 我们还提供了toString方法实现,将在客户端打印此对象时使用。

Next step is to create service classes, so we will have an interface as PersonService and it’s simple implementation class PersonServiceImpl.

下一步是创建服务类,因此我们将有一个接口作为PersonService ,它是简单的实现类PersonServiceImpl

package com.journaldev.jaxws.service;import com.journaldev.jaxws.beans.Person;public interface PersonService {	public boolean addPerson(Person p);		public boolean deletePerson(int id);		public Person getPerson(int id);		public Person[] getAllPersons();}

Below is the implementation service class, we are using Map to store Person objects as data source. In real world programming, we would like to save these into database tables.

下面是实现服务类,我们使用Map将Person对象存储为数据源。 在现实世界的编程中,我们希望将它们保存到数据库表中。

package com.journaldev.jaxws.service;import java.util.HashMap;import java.util.Map;import java.util.Set;import com.journaldev.jaxws.beans.Person;public class PersonServiceImpl implements PersonService {	private static Map
persons = new HashMap
(); @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
ids = persons.keySet(); Person[] p = new Person[ids.size()]; int i=0; for(Integer id : ids){ p[i] = persons.get(id); i++; } return p; }}

That’s it for our business logic, since we will use these in a web service, there is no point of creating web pages here. Notice that we have no reference to any kind of web services classes in above code.

就我们的业务逻辑而言,就是这样,因为我们将在Web服务中使用它们,所以这里没有必要创建网页。 注意,上面的代码中没有引用任何类型的Web服务类。

使用Eclipse的Java中的SOAP Web服务 (SOAP Webservices in Java using Eclipse)

Once our business logic is ready, next step is to use Eclipse to create a web service application from this. Create a new project and select Web Service wizard.

一旦我们的业务逻辑准备就绪,下一步就是使用Eclipse从中创建Web服务应用程序。 创建一个新项目,然后选择Web服务向导。

Click Next button and you will get a page where web service and it’s client details have to be provided. This is the most important page in creating web service. Make sure you select “Web Service type” as “Bottom up Java bean Web Service” because we are implementing with bottom up approach. There are two ways to create web service:

单击下一步按钮,您将获得一个必须提供Web服务及其客户端详细信息的页面。 这是创建Web服务时最重要的页面。 确保您选择“ Web服务类型”作为“自下而上的Java bean Web服务”,因为我们采用的是自下而上的方法。 有两种创建Web服务的方法:

  1. Contract last or Bottom up approach: In this approach we first create the implementation and then generate the WSDL file from it. Our implementation fits in this category.

    契约最后或自下而上的方法:在这种方法中,我们首先创建实现,然后从中生成WSDL文件。 我们的实现适合此类。
  2. Contract first or Top Down Approach: In this approach, we first create the web service contract i.e. WSDL file and then create the implementation for it.

    合同优先或自上而下的方法:在这种方法中,我们首先创建Web服务合同(即WSDL文件),然后为其创建实现。

In the service implementation, provide the implementation class PersonServiceImpl fully classified path. Make sure you move the slider in service and client type to left side so that it can generate client program and also UI to test our web service. Check for the configurations in web service implementation, you should provide correct details for Server runtime, Web service runtime and service project. Usually they are auto populated and you don’t need to make any changes here.

在服务实现中,提供实现类PersonServiceImpl完全分类的路径。 确保将服务和客户端类型中的滑块移到左侧,以便它可以生成客户端程序以及用于测试我们的Web服务的UI。 检查Web服务实现中的配置,您应该提供有关服务器运行时,Web服务运行时和服务项目的正确详细信息。 通常,它们是自动填充的,您无需在此处进行任何更改。

For client configurations, you can provide the client project name as you like. I have left it to default as SOAPExampleClient. If you will click on the link for web service runtime, you will get different options as shown in below image. However I have left it as the default one.

对于客户端配置,您可以根据需要提供客户端项目名称。 我将其默认设置为SOAPExampleClient 。 如果您单击Web服务运行时的链接,您将获得不同的选项,如下图所示。 但是,我将其保留为默认值。

Click on Next button and then you will be able to choose the methods that you want to expose as web service. You will also be able to choose the web service style as either document or literal. You can change the WSDL document name but it’s good to have it with implementation class name to avoid confusion later on.

单击下一步按钮,然后您将能够选择要作为Web服务公开的方法。 您还可以选择文档或文字形式的Web服务样式。 您可以更改WSDL文档名称,但是最好将它与实现类名称一起使用,以免日后造成混淆。

Click on Next button and you will get server startup page, click on the “Start server” button and then next button will enable.

单击下一步按钮,您将获得服务器启动页面,单击“启动服务器”按钮,然后将启用下一步按钮。

Click on Next button and you will get a page to launch the “Web Services Explorer”.

单击下一步按钮,您将获得一个页面以启动“ Web Services Explorer”。

Click on Launch button and it will open a new window in the browser where you can test your web service before moving ahead with the client application part. It looks like below image for our project.

单击启动按钮,它将在浏览器中打开一个新窗口,您可以在继续客户端应用程序部分之前测试Web服务。 看起来像我们项目的下图。

We can do some sanity testing here, but for our simple application I am ready to go ahead with client application creation. Click on the Next button in the Eclipse web services popup window and you will get a page for source folder for client application.

我们可以在这里进行一些健全性测试,但是对于我们的简单应用程序,我准备继续进行客户端应用程序的创建。 在Eclipse Web服务弹出窗口中单击“下一步”按钮,您将获得一个用于客户机应用程序的源文件夹的页面。

Click on Next button and you will get different options to choose as test facility. I am going ahead with JAX-RPC JSPs so that client application will generate a JSP page that we can use.

单击下一步按钮,您将获得不同的选项来选择作为测试工具。 我将继续使用JAX-RPC JSP,以便客户端应用程序将生成我们可以使用的JSP页面。

Notice the methods getEndpoint() and setEndpoint(String) added that we can use to get the web service endpoint URL and we can set it to some other URL in case we move our server to some other URL endpoint.

注意,添加了方法getEndpoint()setEndpoint(String) ,我们可以使用它们获取Web服务端点URL,并且可以将其设置为其他URL,以防我们将服务器移至其他URL端点。

Click on Finish button and Eclipse will create the client project in your workspace, it will also launch client test JSP page as shown below.

单击Finish按钮,Eclipse将在您的工作空间中创建客户端项目,它还将启动客户端测试JSP页面,如下所示。

You can copy the URL and open in any browser you would like. Let’s test some of the services that we have exposed and see the output.

您可以复制URL并在所需的任何浏览器中打开。 让我们测试一些我们已经公开的服务并查看输出。

Eclipse SOAP Web服务测试 (Eclipse SOAP Web Service Test)

  • addPersonaddPerson
  • getPersongetPerson
  • getAllPersons

    Notice that Person details are not printed in the results section, this is because it’s auto generated code and we need to refactor it a little to get the desired output.

    Open Result.jsp in the client project and you will see it’s using switch case to generate the result output. For getAllPersons() method, it was case 42 in my case. Note that it could be totally different in your case. I just changed the code for case 42 as shown below.

    After that we get below output, note that Eclipse is doing hot deployment here, so I didn’t had to redeploy my application.

    getAllPersons

    请注意,“结果”部分未打印“人员”详细信息,这是因为它是自动生成的代码,我们需要对其进行一些重构以获取所需的输出。

    在客户端项目中打开Result.jsp,您将看到它使用开关盒来生成结果输出。 对于getAllPersons()方法,在我的案例中是案例42。 请注意,您的情况可能会完全不同。 我只是更改了案例42的代码,如下所示。

    case 42:        gotMethod = true;        com.journaldev.jaxws.beans.Person[] getAllPersons42mtemp = samplePersonServiceImplProxyid.getAllPersons();if(getAllPersons42mtemp == null){%><%=getAllPersons42mtemp %><%}else{        String tempreturnp43 = null;        if(getAllPersons42mtemp != null){        java.util.List
    listreturnp43= java.util.Arrays.asList(getAllPersons42mtemp); //tempreturnp43 = listreturnp43.toString(); for(com.journaldev.jaxws.beans.Person p : listreturnp43){ int id = p.getId(); int age = p.getAge(); String name=p.getName(); %> <%=id%>::<%=name %>::<%=age %> <% } } } break;

    之后,我们得到下面的输出,请注意,Eclipse在这里进行热部署,因此我不必重新部署应用程序。

So it looks like our web service and client applications are working fine, make sure to spend some time in looking at the client side stubs generated by Eclipse to understand more.

因此,看来我们的Web服务和客户端应用程序运行良好,请确保花一些时间查看Eclipse生成的客户端存根以了解更多信息。

SOAP Web服务WSDL和配置 (SOAP Web Service WSDL and Configs)

Finally you will notice that WSDL file is generated in the web service project as below.

最后,您会注意到WSDL文件是在Web服务项目中生成的,如下所示。

PersonServiceImpl.wsdl code:

PersonServiceImpl.wsdl代码:

If you will open it in design mode in Eclipse, it will look like below image.

如果要在Eclipse中以设计方式打开它,它将看起来像下面的图像。

You can also access web service WSDL file through browser by appending ?wsdl to the web service endpoint.

您还可以通过将?wsdl附加到Web服务端点来通过浏览器访问Web服务WSDL文件。

You will also note that web.xml is modified to use Apache Axis as front controller for web service.

您还将注意到,已将web.xml修改为使用Apache Axis作为Web服务的前端控制器。

SOAPExample
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
Apache-Axis Servlet
AxisServlet
org.apache.axis.transport.http.AxisServlet
AxisServlet
/servlet/AxisServlet
AxisServlet
*.jws
AxisServlet
/services/*
Axis Admin Servlet
AdminServlet
org.apache.axis.transport.http.AdminServlet
100
AdminServlet
/servlet/AdminServlet

Below image shows the web service and client project with all the auto generated stubs and JSP pages to test the web service.

下图显示了Web服务和客户端项目,以及所有自动生成的存根和JSP页面来测试Web服务。

That’s all for soap webservices in java example using Eclipse, as you can see that all the hard part was done by Eclipse automatically and all our focus was to write business logic for our web service.

这就是使用Eclipse的Java示例中的soap webservices的全部内容,您可以看到所有困难的部分都是由Eclipse自动完成的,而我们的全部重点是为我们的Web服务编写业务逻辑。

翻译自:

转载地址:http://atlzd.baihongyu.com/

你可能感兴趣的文章
CYQ.Data V5 MAction新增加SetExpression方法说明
查看>>
数据安全&MD5加密
查看>>
bzoj 2594: 水管局长数据加强版 Link-Cut-Tree
查看>>
世界是数字的观后感
查看>>
由DBCursor的“can't switch cursor access methods”异常引发的思考
查看>>
LUOGU P1438 无聊的数列 (差分+线段树)
查看>>
引用和指针的区别
查看>>
stm32 usart 异步传输示例
查看>>
yum 安装过程下载的包存放路径
查看>>
二叉树
查看>>
idea下http响应乱码
查看>>
jquery使用$.each()
查看>>
Sybase 15.7 开发版下载(非注册)
查看>>
P1527 [国家集训队]矩阵乘法
查看>>
java 包(package)
查看>>
android Service介绍
查看>>
[MySQL 5.6] GTID实现、运维变化及存在的bug
查看>>
css钻石旋转实现
查看>>
sencha touch list infinite 属性
查看>>
指令——cat
查看>>