Open In App

Java Web Services

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The services that are accessible across various networks are Java Web Services. Since JavaEE 6, it has two APIs defined by Java for creating web services applications.

  • JAX-WS for SOAP web services
  • JAX-RS for RESTful web services.

It is not necessary to add any jars in order to work with either of these APIs because they both use heavy annotations and are included in the standard JDK.

JAX-WS

The Jakarta EE API, known as JAX-WS, is used to develop and create web services, especially for SOAP service users. The development and deployment of web services clients and endpoints are made simpler by using annotations.

Two ways to write JAX-WS application code are:

  • RPC Style
  • Document Style

NOTE: JAX-RPC API was superseded by JAX-WS 2.0.

JAX-RS

JAX-RS is a framework for building RESTful web applications. Currently, there are two implementations for building JAX-RS :

  • Jersey
  • RESTeasy

Implementation of Java Web Services

1. Implementing SOAP Web Services with JAX-WS

There are certain steps to implement SOAP Web Services with JAX-WS

  1. First, you need to define Service endpoint interfaces (SEI) which specify the methods to expose as web service.
  2. Next, you need to implement SEI with a Java class.
  3. Then you need to Annotate the SEI and its implementation class with JAX-WX annotations to specify the web service details.
  4. Package web service classes to the WAR file and deploy it to a web server.

2. Implementing RESTful Web Services with JAX-RS

There are certain steps to implement RESTful Web Services with JAX-RS

  1. First you need to define the resources that represents the web services and its methods.
  2. Then you need to annotate the resource class and its methods with JAX-RS annotations to specify the web service package.
  3. At last we need to package the web service classes to the WAR file and deploy it to a web server.

Examples of Java Web Services

This is an example of how we can use JAX-WS to create a Java Web Service (JWS) using the data from the search results.

1. The SEI (Service Endpoint Interface), which shows the procedures of web services

Java




import javax.jws.WebMethod;
import javax.jws.WebService;
  
@WebService
public interface HelloWorld {
    @WebMethod
    String sayHello(String name);
}


2. Implement the SEI with java class

Java




import javax.jws.WebService;
  
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}


3. Annotate the SEI and it’s implementation class with JAX-WX annotations to specify the web services.

Java




import javax.jws.WebMethod;
import javax.jws.WebService;
  
@WebService
public interface HelloWorld {
    @WebMethod
    String sayHello(String name);
}
  
import javax.jws.WebService;
  
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}


Frequently Asked Questions

1. How does a web service work?

A web service is mode of communication among various application by using some open standard like HTML, SOAP, XML. You can develop java based service on Solaris that is accessible from Visual Basic program that runs on windows.

2. What is SOAP?

SOAP stands for Simple Object Access Protocol. An XML based protocol used for accessing web services. It is independent from platform and language. By using SOAP you can interact with other programming.

3. What is RESTful web services?

RESTful stands for Representational State Transfer. It is an architectural style. It is not protocol similar to SOAP. These services are fast because there is no strict specification like SOAP. It uses less data and bandwidth and resources.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads