Web-service :- Its a service built and deployed in server. Which can be accessed by any type of application irrespective of language platform. For more information please refer to my previous tutorials.
Service end point interface :- Create an interface which a blue print for your web-service. If you annotate your Interface with @WebService then your interface is eligible for web service. If you annotate your methods as @WebMethod then methods inside the interface are exposed as web-service methods.
package com.krishna.webservice.basics;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface JoiningFormalities {
@WebMethod
public String getEmployeeRole();
@WebMethod
public String showWelcomeGreetings();
}
Service implementation bean :- We need to create implementation class for our service interface. If we annotate our class with @webservice and point it to our interface then it will become implementation of our service.
Service end point interface :- Create an interface which a blue print for your web-service. If you annotate your Interface with @WebService then your interface is eligible for web service. If you annotate your methods as @WebMethod then methods inside the interface are exposed as web-service methods.
package com.krishna.webservice.basics;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface JoiningFormalities {
@WebMethod
public String getEmployeeRole();
@WebMethod
public String showWelcomeGreetings();
}
Service implementation bean :- We need to create implementation class for our service interface. If we annotate our class with @webservice and point it to our interface then it will become implementation of our service.
package com.krishna.webservice.basics;
import javax.jws.WebService;
@WebService(endpointInterface="com.krishna.webservice.basics.JoiningFormalities")
public class NewJoinesImpl implements JoiningFormalities {
@Override
public String getEmployeeRole() {
return "Team Lead";
}
@Override
public String showWelcomeGreetings() {
return "Welcome Krishna";
}
}
Publishing web-service :- Generally we publish webservices on application servers but using Endpoint class we can publish to our personal computer. (127.0.0.1) is our computer loopback address.
package com.krishna.webservice.basics;
import javax.xml.ws.Endpoint;
public class PublishWebservice {
public static void main(String args[]){
Endpoint.publish("http://127.0.0.1:9876/ws", new NewJoinesImpl());
}
}
Testing Webservice by opening a browser :-
WSDL file :-
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://basics.webservice.krishna.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://basics.webservice.krishna.com/" name="NewJoinesImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://basics.webservice.krishna.com/" schemaLocation="http://127.0.0.1:9876/ws?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="getEmployeeRole">
<part name="parameters" element="tns:getEmployeeRole"></part>
</message>
<message name="getEmployeeRoleResponse">
<part name="parameters" element="tns:getEmployeeRoleResponse"></part>
</message>
<message name="showWelcomeGreetings">
<part name="parameters" element="tns:showWelcomeGreetings"></part>
</message>
<message name="showWelcomeGreetingsResponse">
<part name="parameters" element="tns:showWelcomeGreetingsResponse"></part>
</message>
<portType name="JoiningFormalities">
<operation name="getEmployeeRole">
<input message="tns:getEmployeeRole"></input>
<output message="tns:getEmployeeRoleResponse"></output>
//List of operations under this webservice
</operation>
<operation name="showWelcomeGreetings">
<input message="tns:showWelcomeGreetings"></input>
<output message="tns:showWelcomeGreetingsResponse"></output>
</operation>
</portType>
<binding name="NewJoinesImplPortBinding" type="tns:JoiningFormalities">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="getEmployeeRole">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
<operation name="showWelcomeGreetings">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
//Service end point declaration
<service name="NewJoinesImplService">
<port name="NewJoinesImplPort" binding="tns:NewJoinesImplPortBinding">
<soap:address location="http://127.0.0.1:9876/ws"></soap:address>
</port>
</service>
</definitions>
No comments:
Post a Comment