annotations used in JAX WS API

annotations used in JAX WS API

The JAX-WS (Java API for XML Web Services) API uses several annotations to simplify the creation and consumption of SOAP web services. Here are some of the most important annotations:

annotations used in JAX WS API

1.  @WebService

Indicates that the class implements a web service, typically used at the class level.

2.  @WebMethod

Specifies that a method should be exposed as a web service operation.

3.  @WebParam

Used to customize the mapping of an individual parameter to a web service message part.

4.  @WebResult

Customizes the mapping of the return value to a web service message part.

5.  @SOAPBinding

Customizes the SOAP binding of the web service, including style, use, and parameter style.

6.  @HandlerChain

Specifies a file containing a list of handlers that are invoked before and after the web service method is called.

Explanation in Java Example

Let’s create a simple SOAP web service using JAX-WS annotations to demonstrate their usage.

1.  Maven Dependencies (pom.xml)
xml
<dependencies>
    <dependency>
        <groupId>javax.xml.ws</groupId>
        <artifactId>jaxws-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

2.  Service Endpoint Implementation (CalculatorService.java) :

Create a class to implement the web service using the JAX-WS annotations.

Syntax
java
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;

@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL)
public class CalculatorService {

    @WebMethod(operationName = "addition")
    @WebResult(name = "result")
    public int add(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        return a + b;
    }

    @WebMethod(operationName = "subtraction")
    @WebResult(name = "result")
    public int subtract(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        return a - b;
    }

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/ws/calculator", new CalculatorService());
    }
}

Explanation

  • 1.  @WebService :
    • The CalculatorService class is annotated with @WebService, indicating that it provides a web service.
  • 2.  @SOAPBinding :
    • The @SOAPBinding annotation customizes the SOAP binding for the web service. In this example, the style is set to DOCUMENT and the use is set to LITERAL.
  • 3.  @WebMethod :
    • The add and subtract methods are annotated with @WebMethod, exposing them as web service operations.
    • The operationName attribute specifies the name of the operation as it appears in the WSDL.
  • 4.  @WebParam :
    • The parameters of the add and subtract methods are annotated with @WebParam, customizing their names in the SOAP message.
  • 5.  @WebResult :
    • The @WebResult annotation customizes the name of the return value in the SOAP message.