-
Create a sample web service as below
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface TestWS {
@WebMethod
public abstract String getMethod(String param);
}
-
Implement the web service
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(endpointInterface="org.myProject.TestWS",serviceName="TestWS")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT)
public class TestWSImpl implements TestWS{
@WebMethod
public String getMethod(@WebParam(name="param")String param) {
System.out.println("test ws");
return "test";
}
}
We have linked TestWSImpl class to TestWS using annotation param endpointInterface="org.myProject.TestWS" . Now,as we know, web service is a communication between two machines via xml messages , so an endpoint is reference to which messages are addressed.
-
Host the web service
Service name is to be mapped in web.xml as below:
<servlet>
<servlet-name>PasswordManagerWSImpl</servlet-name>
<servlet-class>org.myProject.passwordManager.PasswordManagerWSImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PasswordManagerWSImpl</servlet-name>
<url-pattern>/TestWS</url-pattern>
</servlet-mapping>
Once the server is up and running , you can verify whether the web service is hosted properly
Go the following url
http://localhost:8089/TestWebService/TestWS?wsdl
Replace TestWebService with the name of WAR file deployed.
If on hitting the link you are able to get the WSDL then the web service is successfully hosted.
WSDL has the details of the web service which is used by the client to communicate to the web service.
- Client Interaction with the web service
URL url=new URL("http://localhost:8089/TestWebService/TestWS?wsdl");
Service service=Service.create(url,qName);
TestWS webSer=service.getPort(TestWS.class);
String test=webSer.getMethod("test");
URL object is created by passing url of WSDL.
References: