Web services is vast topic ,
lets start with creating a simple web service.I have tried explaining the concepts using a sample code ,In any point if you are unable to understand please go through the documentation links .
-
Create a sample web service as below
package org.myProject;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface TestWS {
@WebMethod
public abstract String getMethod(String param);
}
TestWS is a web service
-
Implement the web service
import javax.jws.WebMethod;
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";
}
}
In addition to implementing the method
getMethod , a service name is also specified for the web service.
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.
There are several ways to host your web service.I choose to host it through jboss EAP 6.The web service need to be packaged in war file and deployed to jboss .
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
QName qName=new QName("http://myProject.org/","TestWS");
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");
A QName object is created by passing namespaceURI as targetNamespace and localPost as service name from WSDL.
URL object is created by passing url of WSDL.
References: