Bootstrap FreeKB - Java - Create a web service in Eclipse
Java - Create a web service in Eclipse

Updated:   |  Java articles

A classic web application does not create separation between the backend and the frontend. To use an analogy, imagine a coffee shop. A classic web application would be responisble for brewing the coffee (backend) and placing the order for the coffee (frontend). A web service creates separation between the backend and frontend. With a web service, the backend application (web service) brews the cup of coffee, and the frontend application (web client) places the order for the cup of coffee. 

Full disclaimer - I loves me my coffee.

Let's see this in action. Create a dynamic web project (WebServiceDemo in this example).

 

Select WindowPreferences > Web Services > Server and Runtime. If the dynamic web project is configured to run on WebSphere Liberty, then the web service runtime should be IBM WebSphere JAX-WS. If the dynamic web project is configured to run on Apache Tomcat, then the web service runtime should be Apache Axis. 

 

Start the server. This is being done because we are going to be testing the web service on the server runtime in Eclipse.

 


Create a package 

In Eclipse, expand Java Resources. Right-click on the src folder and select New > Package. Give the package a name, such as com.example, and select Finish.

 


Create a class

Right click on the com.example package, and select New Class. Give the class a name, such as coffee, and select Finish.

 

Add the following markup to the class.

package com.example;

public class coffee {
  public String typeOfCoffee(String brew) {
    return "Type of coffee = " + brew;
  } 
}

 


Update the class to be a web service

Right-click on the coffee.java class, and select Web Services Create Web Service. In the dialog box, make the following selections and select Next.

 

It's going to take a while to create the service. You should get the following pop-up a few times. This is a good sign.

 

If no problems occur, you should get a view like this after the web service has been created. This is the web service client running on the server runtime in Eclipse. Selecting the typeOfCoffee method, you can test the service. In this example, "Iced Coffee" is successfully requested and returned. In other words, the service is working.

 

By the way, the above JSP is found at the following location in case you close out of Eclipse, you can get to the JSP page again.

 

You should now see 4 projects in Eclipse, which is actually just two WARs (the client and web service) and two EARs. To run the service on an application server, export WebServiceDemo.war and WebServiceDemoClientEAR.ear.

 

In this example, WebServiceDemo.war and WebServiceDemoClientEAR.ear have been deployed to a WebSphere application server.

 

In this example, after deployed to WebSphere, I am able to get to the TestClient.jsp page, request Latte, and get a response from the service. 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ba74cd in the box below so that we can be sure you are a human.