WebServices- JAX-WS Annotations

JAX-WS Annotations

We have following important annonotations in order to workwith JAX-WS webservices. They are

  1. @WebService

  2. @SoapBinding

  3. @WebMethod

  4. @WebResult

  5. @WebServiceClient

  6. @RequestWrapper

  7. @ResponseWrapper

  8. @Oneway

  9. @HandlerChain

1.@WebService

This annotation can be used in 2 ways

a.To mark the class as the implementing the Web Service

Package webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface HelloWorld {
	@WebMethod
	String getHelloworldMessage(String msg);
}

b. Defining a Web Service Interface (SEI), in other words Service Endpoint Interface

import javax.jws.WebService;

@WebService(endpointInterface="webservice.HelloWorld ")
public class HelloWorldImpl implements HelloWorld{
	@Override
	public String getHelloworldMessage (String name) {
 return "Hello World JAX-WS " + name;
	}
}

@Webservice with all attributes as below formate

@WebService(portName = "SoapPort", serviceName = " HelloWorld ",
 targetNamespace = "http://apache.org/hello_world_soap_http",
 endpointInterface="webservice.HelloWorld ")

2.@SoapBinding

This annotation is used to specify the SOAP messaging style which can either be RPC or DOCUMENT

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
//@SOAPBinding(style = Style. DOCUMENT)
public interface HelloWorld{
	@WebMethod 
String getHelloWorldAsString(String name);
}

@SoapBinding with all attributes as below formate

@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
             use=SOAPBinding.Use.LITERAL,
             parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)

3.@WebMethod

@WebMethod JAX-WS annotation can be applied over a method only. This specified that the method represents a web service operation.it will be used in Interface (Service Endpoint Interface) level method only, not in implementation method level.()

//Service Endpoint Interface
@WebService 
public interface HelloWorld{
	@WebMethod 
String getHelloWorldAsString(String name);
}

@WebMethod with all attributes as below formate

@WebMethod(operationName="echoComplexType", action=" SOAPAction")

4.@WebResult

@WebResult can be used to determine what the generated WSDL shall look like

@WebService
public interface HelloWorld{
@WebMethod
@WebResult(partName="Helloworld Method") 
String getHelloWorldAsString(String name);
}
//Service Implementation
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
	@Override
	public String getHelloWorldAsString(String name) {
 return "Hello World JAX-WS " + name;
	}
}
public class WSPublisher {
	public static void main(String[] args) {
 Endpoint.publish("http://127.0.0.1:9999/ctf", new getHelloWorldAsString ());
	}
}

On publishing the generated WSDL (at URL: http://127.0.0.1:9999/ctf?wsdl) would be like:

<definitions
	xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
	xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://webresult.jaxWsAnnotations.examples.smlcodes.com/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"
	targetNamespace="http://webresult.jaxWsAnnotations.examples.smlcodes.com/"
	name="WSAnnotationsWebResultImplService">
	<types />
	<message name=" getHelloWorldAsString ">
 <part name="arg0" type="xsd: string " />
	</message>
	<message name=" Helloworld Method ">
 <part name=" getHelloWorldAsString " type="xsd:string" />
	</message>
	
</definitions>

5.@WebServiceClient

@WebServiceClient(
name = "WsAnnotationsWebServiceImplService", 
targetNamespace = "http://webservice.smlcodes.com/", wsdlLocation = "file:/Users/satyakaveti/Downloads/ctf.wsdl")

The information specified in this annotation helps in identifying a wsdl: service element inside a WSDL document. This element represents the Web service for which the generated service interface provides a client view.

6.@RequestWrapper

@RequestWrapper JAX-WS annotation is used to annotate methods in the Service Endpoint Interface with the request wrapper bean to be used at runtime.

It has 4 optional elements; className that represents the request wrapper bean name, localName that represents element’s local name, partName that represent the part name of the wrapper part in the generated WSDL file, and** targetNamespace** that represents the element’s namespace

@WebService
@SOAPBinding(style=Style.RPC)
public interface WSRequestWrapperInterface {
	@WebMethod
	@RequestWrapper(localName="CTF", 
	targetNamespace="http://smlcodes.com/tempUtil", 
	className="com.smlcodes.examples.jaxWsAnnotations.webservice.CTF")
	float celsiusToFarhenheit(float celsius);
}

7.@ResponseWrapper

@ResponseWrapper JAX-WS annotation is used to annotate methods in the Service Endpoint Interface with the response wrapper bean to be used at runtime. It has 4 optional elements; className that represents the response wrapper bean name, localName that represents element’s local name, partName that represent the part name of the wrapper part in the generated WSDL file, and targetNamespace that represents the element’s namespace.

public interface WSResponseWrapperInterfaceI {
	@WebMethod
	@ResponseWrapper(localName="CTFResponse", 
	targetNamespace="http:// smlcodes.com/tempUtil", 
	className="com. smlcodes.examples.jaxWsAnnotations.webservice.CTFResponse")
	float celsiusToFarhenheit(float celsius);
}

8.@Oneway

@Oneway JAX-WS annotation is applied to WebMethod which means that method will have only input and no output. When a @Oneway method is called, control is returned to calling method even before the actual operation is performed. It means that nothing will escape method neither response neither exception.

@WebService
@SOAPBinding(style = Style.RPC)
public interface WSAnnotationsOnewayI {
	@WebMethod
	@Oneway
	void sayHello();
}

9.@HandlerChain

Web Services and their clients may need to access the SOAP message for additional processing of the message request or response. A SOAP message handler provides a mechanism for intercepting the SOAP message during request and response.

A handler at server side can be a validator. Let’s say we want to validate the temperature before the actual service method is called. To do this our validator class shall implement interface SOAPHandler

package handler;

public class TemperatureValidator implements SOAPHandler {

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
 // TODO Auto-generated method stub
 return false;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
 // TODO Auto-generated method stub
 return false;
	}

	@Override
	public void close(MessageContext context) {
 // TODO Auto-generated method stub
 
	}

	@Override
	public Set getHeaders() {
 // TODO Auto-generated method stub
 return null;
	}

}
// soap-handler.xml 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<javaee:handler-chain>
 <javaee:handler>
 	<javaee:handler-class>com.smlcodes.examples.jaxWsAnnotations.handler.TemperatureValidator
 	</javaee:handler-class>
 </javaee:handler>
	</javaee:handler-chain>
</javaee:handler-chains>
package handler;

@WebService
@SOAPBinding(style = Style.RPC)
public interface WSAnnotationsHandlerChainI {
	@HandlerChain(file = "soap-handler.xml")
	@WebMethod
	float celsiusToFarhenheit(float celsius);
}