In this post we’ll learn how to create Java based Web Service/Client in Eclipse. Webservices are widely used between different application to communicate with each other and pass information. There are multiple ways to create web services in Java. This is one of those and quite simple example
Create a Web Service
Create a dynamic web project in eclipse. Write a Java class. For this example I am writing a simple Car class with a method to return a car name. When Web service will be called to get car name then following methid will return ferrari in response to API call.
package com.webservice; public class Car { public String getName() { return "ferrari"; } }
Now right click on this project and create –> New –> WebService.
Select your class in browse. Click next and finish. You will see wsdl file in web-inf folder.
Create web service client
Create new dynamic web project
Now right click on this project and create –> New –> WebServiceClient.
Give path or url to wsdl file. Click next and finish. It will create necessary files and classes.
There will be java class with name like *proxy*. Create this class object in jsp page or servlet and call any of its method. These will be those methods which you defined in Car class.