Thursday August 23, 2007
TOTD #4: How to convert a Session EJB to a Web service ?
This TOTD describes how to convert a stateless session EJB to a Web service and uses information from this thread.
@javax.jws.WebService annotation at the top of your EJB
class. The modified code looks like:@javax.ejb.Stateless
@javax.jws.WebService
public class HelloSessionBean implements server.HelloSessionLocal {
public String sayHello(String name) {
return "Hello " + name + " from
session bean";
}
}
The new annotation is shown in this color.That's it!
There is no need to specify any additional deployment descriptor or
parameters.The WSDL exposed by the EJB Web service endpoint is available at "http://localhost:8080/HelloSessionBeanService/HelloSessionBean?wsdl".
The generated WSDL looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS
RI 2.1.2-hudson-182-RC1. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS
RI 2.1.2-hudson-182-RC1. -->
<definitions
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://server/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://server/"
name="HelloSessionBeanService">
<wsp:UsingPolicy></wsp:UsingPolicy>
<wsp:Policy wsu:Id="HelloSessionBeanPortBinding_sayHello_WSAT_Policy">
<wsp:ExactlyOne>
<wsp:All>
<ns1:ATAlwaysCapability
xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/10/wsat" wsp:Optional="false"></ns1:ATAlwaysCapability>
<ns2:ATAssertion xmlns:ns3="http://schemas.xmlsoap.org/ws/2002/12/policy"
xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/10/wsat" ns3:Optional="true"
wsp:Optional="true"></ns2:ATAssertion>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<types>
<xsd:schema>
<xsd:import namespace="http://server/"
schemaLocation="http://localhost:8080/HelloSessionBeanService/HelloSessionBean?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloSessionBean">
<operation name="sayHello">
<input message="tns:sayHello"></input>
<output message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloSessionBeanPortBinding" type="tns:HelloSessionBean">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document"></soap:binding>
<operation name="sayHello">
<wsp:PolicyReference URI="#HelloSessionBeanPortBinding_sayHello_WSAT_Policy"></wsp:PolicyReference>
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloSessionBeanService">
<port name="HelloSessionBeanPort" binding="tns:HelloSessionBeanPortBinding">
<soap:address location="http://localhost:8080/HelloSessionBeanService/HelloSessionBean"></soap:address>
</port>
</service>
</definitions>
Few points to notice:
A reasonable set of defaults are chosen for portType/@name,
binding/@name, service/@name and even the soap:address/@location. Most of these values can be changed by specifying
a different value in the @WebService annotation.
Accordingly to EJB 3.0 specification, if @TransactionAttribute
is not specified on the method then a default value of REQUIRED
is applied. This default value is automatically converted to
ATAlwaysCapability and ATAssertion policy assertions.
Accordingly to
Web Services for Java EE, Version 1.2, webservices.xml is
optional so there is no need to write any other deployment descriptor.
Be careful not to deploy a WAR file with the context root
generated (HelloSessionBeanService) for the Web service
endpoint. The EJB Web service endpoint will be inaccessible after that.
Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.
Technorati: totd webservices ejb glassfish
Posted by Arun Gupta in webservices | Comments[0]
|
|
|
|
|
Today's Page Hits: 2455
Total # blog entries: 1025