Java Web Service using Eclipse (Eclipse提供的自动生成webservice接口,endpoint的功能,以及自动测试webservice的功能)

转自:  https://javapapers.com/web-service/java-web-service-using-eclipse/

This tutorial is to create a java web service and client using Eclipse IDE. We will go by lazy way and use Eclipse wizard to generate the web service and client. Previously I wrote a tutorial to create a java web service using NetBeans and based on popular request I am writing this tutorial.

There are two ways to develop a web service namely top-down approach and bottom-up approach. To know about these approaches and in general about web service refer my introduction to web service tutorial posted earlier. We will be using bottom-up development approach in this tutorial.

Eclipse has many different versions and remember to use the Java EE IDE. It comes bundled with necessary plugins.

Just for your information, Eclipse by default uses Apache Axis to implement the web service and it provides option to use our choice of web service engine. I decided to go with the default bundled Apache Axis.

1. Create a Dynamic Web Project

Use the new project from menu and open project wizard. Select ‘Dynamic Web Project’ and click next. Then give a project name and select a target runtime (I have Apache Tomcat v7.0) and leave all other default values and click next to finish.


Create a New Runtime

If the run time is not already defined, then click New Runtime and select the version of Tomcat you have installed (already) then click next. Now browse the path of Tomcat home directory and click finish.

2. Create Web Service Provider Java Class

Create a new package under ‘Java Resources – src’ named com.javapapers.jee Then, create a new java class under that package. This is the web service’s service provider class.

package com.javapapers.jee;

public class AnimalTypeService {
	public String animalType(String animal) {
		String animalType = "";
		if ("Lion".equals(animal)) {
			animalType = "Wild";
		} else if ("Dog".equals(animal)) {
			animalType = "Domestic";
		} else {
			animalType = "I don't know!";
		}
		return animalType;
	}
}

I have used the above for my service. This is a simple service which will return type of animal based on argument passed.

With this step everything is over. If you have Eclipse it will take care of all the remaining work like creating web service, generating wsdl, skeleton, service client, stub and etc etc. Imagine how it would be if we have an Eclipse to help us in living our daily life. Eclipse for bathing, eating, … just sit and click eating is done. Crazy isn’t it? That is how the remainder of web service creation is going to be, just click click. IDE is helping us to do our job better but sometimes I think it makes us lazy. If you are a beginner, you must know what is happening behind the screens for every click in the Eclipse wizard.

3. Create a Web Service

Now the service class is ready and we need to create a web service using this java class.

Right click on ‘Java Resources’ -> New and select ‘Web Service’ under ‘Web Services’ folder from the wizard. Click Next button.

Select Service Implementation

In this Web Service wizard, use the browse button and select the java class written earlier. Which is our service implementation java class.
Then, drag the slider bar to upper most in both service and client part.
Then, enable the Publish the Web service check box.

Web Service Client Creation

Added to configuring the service implementation, we are instructing Eclipse to generate a web service client also. This will create a dynamic web java project and create a web service client for the web service created. Instead we can also create a java project and write a client to access the web service.

Deploy Web Service and Client

Click Next till the Server Starup wizard and then click Start Server. This step will start the associated runtime Tomcat.

4. Test Web Service

We can test the web service using a web service client. For this we need to create web service client application. In our previous configuration we moved the slider above in service configuration wizard which will create a web service client application.

Eclipse Web Service Explorer


There is one more way, Eclipse provides a Web Service Explorer. It is a nice client that will help us test a web service client.

Then click Next to reach ‘Test Web Service’ wizard and then click Launch button.

Web Service Client Application

Then click next next and we will see the web service client application generated. See in the image below, EclipseWebServiceClient is a web application generated by Eclipse.

Do not stop with this. Open the EclipseWebServiceClient application and go through the code and generated Stub, Proxy and all the files. It will really help to understand more.

Download Eclipse based Web Service Source Code

Download Eclipse based Web Service Client Source Code

This Web Service tutorial was added on 05/11/2012.


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值