In this java tutorial, you will understand how to develop web services in java
WEB SERVICES
- It is a type of web application which is used to share the message between client and server (Communication between two devices on a network)
- Unlike web applications, the main benefit of web service is code reusability. A single web service can be used by different kinds of applications
- The main component of a web service is the data which is shared between client and server
- It is a client server application for creating communication between two devices over the network for exchanging data.
TYPES OF WEB SERVICES
- Web services come under two categories. They are
- SOAP Based Services (XML Services)
- REST Based Services
- Both services are two different ways to connect applications with server-side data.
1. SOAP Web Services (XML Web Services)
- SOAP stands for Simple Object Access Protocol
- This is an XML based protocol for designing and developing web services.
- It is a platform and language independent as it is XML based
- It is an open standard protocol and provides data transport for web services (Web services use SOAP protocol for sending the XML data between applications)
2. REST Web Services (RESTful Web Services)
- REST stands for REpresentational State Transfer
- It is an architectural style for developing web services
- It is most popular web services on current trends
- Web services that implement REST architecture are called as RESTful web services
- It is designed specifically for the components like media components, files or objects on a particular hardware.
DIFFERENCES BETWEEN SOAP AND REST WEB SERVICES
S.N | SOAP Services | REST Services |
1. | SOAP stands for simple object access protocol (Structured protocol) | REST stands for representational state transfer |
2. | It is a protocol | REST is an architectural style |
3. | It only supports XML format | It supports various formats like HTML, XML, JSON, Plain Text etc,. |
4. | SOAP needs more bandwidth for its usage | REST doesn’t need more bandwidth |
5. | JAX-WS is the java API for SOAP web services | JAX-RS is the java API for REST |
6. | It is more secure than REST | It is less secure than SOAP |
7. | Resources – XML & HTTP | Resources – HTTP |
8. | It is used to transport data in XML format | Here it is used to transport data in JSON format which is based on URI. |
9. | As it is XML based, it works with WSDL (Web Service Description Language) | It works with HTTP GET, POST, PUT, DELETE methods |
10. | It can’t use REST because it is a protocol | REST can use SOAP web services and it can use any protocols like HTTP, SOAP |
11. | It uses service interfaces to expose the business logic | It uses the URI (Uniform Resource Identity) to expose the business logic |
12. | It is less preferred than REST | It is more preferred than SOAP. It is most popular web services on recent days |
I. EXAMPLE OF XML / SOAP WEB SERVICES IN JAVA
STEP 1 – CREATE A NEW PROJECT IN NETBEANS 18
STEP 2 – DETAILS OF PROJECT NAME AND LOCATION
STEP 3 – SERVER SELECTION
STEP 4 – FRAMEWORK SELECTION – OPTIONAL
VERIFICATION OF NEWLY CREATED PROJECT IN NETBEANS
2. SOURCE CODE
(NewWebService.java)
package tp;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = “NewWebService”)
public class NewWebService
{
@WebMethod(operationName = “hello”)
public String hello(@WebParam(name = “name”) String txt) {
return “Hello ” + txt + ” !”;
}
@WebMethod()
public String Add(int a, int b)
{
int c=a+b;
return “Sum is: “+Integer.toString(c);
}
@WebMethod()
public String Sub(int a, int b)
{
int c=a-b;
return “Sub is: “+Integer.toString(c);
}
@WebMethod()
public String Mul(int a, int b)
{
int c=a*b;
return “Mul is: “+Integer.toString(c);
}
@WebMethod()
public String Div(int a, int b)
{
int c=a/b;
return “Div is: “+Integer.toString(c);
}
}
3. OUTPUT
3.1 DEPLOYING THE PROJECT BEFORE TESTING THE WEB SERVICE RIGHT CLICK ON THE CURRENT PROJECT AND SELECT DEPLOY OPTION
3.2 DEPLOYED RESULT IN NETBEANS TERMINAL
3.3 DEPLOYED RESULT IN GLASSFISH SERVER
3.4 DEPLOYED RESULT IN GLASSFISH SERVER – (Continue)
3.5 RUNNING THE WEB SERVICE
3.6 HOME PAGE OF WEB SERVICE – LIST OF AVAILABLE SERVICES
3.7 TESTING WEB SERVICES – ADD WEB METHOD – SUBMISSION OF DYNAMIC INPUTS
3.8 RESULT – XML INTERFACE
3.9 RESULT – XML – SOAP REQUEST & SOAP RESPONSE