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.
- Add
@javax.jws.WebServiceannotation at the top of your EJB
class. The modified code looks like:
The new annotation is shown in this color.@javax.ejb.Stateless<br>
<b><font color="#FF9966">@javax.jws.WebService</font></b><br>
public class HelloSessionBean implements server.HelloSessionLocal {<br>
public String sayHello(String name) {<br>
return "Hello " + name + " from
session bean";<br>
}<br>
}<br>
<br>
- Re-build your project and redeploy it.
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"?><br>
<!-- 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. --><br>
<!-- 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. --><br>
<definitions<br>
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"<br>
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"<br>
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"<br>
xmlns:tns="http://server/"<br>
xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br>
xmlns="http://schemas.xmlsoap.org/wsdl/"<br>
targetNamespace="http://server/"<br>
name="HelloSessionBeanService"><br>
<wsp:UsingPolicy></wsp:UsingPolicy><br>
<wsp:Policy wsu:Id="HelloSessionBeanPortBinding_sayHello_WSAT_Policy"><br>
<wsp:ExactlyOne><br>
<wsp:All><br>
<ns1:ATAlwaysCapability
xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/10/wsat" wsp:Optional="false"></ns1:ATAlwaysCapability><br>
<ns2:ATAssertion xmlns:ns3="http://schemas.xmlsoap.org/ws/2002/12/policy"
<br>
xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/10/wsat" ns3:Optional="true"
<br>
wsp:Optional="true"></ns2:ATAssertion><br>
</wsp:All><br>
</wsp:ExactlyOne><br>
</wsp:Policy><br>
<types><br>
<xsd:schema><br>
<xsd:import namespace="http://server/"
schemaLocation="http://localhost:8080/HelloSessionBeanService/HelloSessionBean?xsd=1"></xsd:import><br>
</xsd:schema><br>
</types><br>
<message name="sayHello"><br>
<part name="parameters" element="tns:sayHello"></part><br>
</message><br>
<message name="sayHelloResponse"><br>
<part name="parameters" element="tns:sayHelloResponse"></part><br>
</message><br>
<portType name="HelloSessionBean"><br>
<operation name="sayHello"><br>
<input message="tns:sayHello"></input><br>
<output message="tns:sayHelloResponse"></output><br>
</operation><br>
</portType><br>
<binding name="HelloSessionBeanPortBinding" type="tns:HelloSessionBean"><br>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document"></soap:binding><br>
<operation name="sayHello"><br>
<wsp:PolicyReference URI="#HelloSessionBeanPortBinding_sayHello_WSAT_Policy"></wsp:PolicyReference><br>
<soap:operation soapAction=""></soap:operation><br>
<input><br>
<soap:body use="literal"></soap:body><br>
</input><br>
<output><br>
<soap:body use="literal"></soap:body><br>
</output><br>
</operation><br>
</binding><br>
<service name="HelloSessionBeanService"><br>
<port name="HelloSessionBeanPort" binding="tns:HelloSessionBeanPortBinding"><br>
<soap:address location="http://localhost:8080/HelloSessionBeanService/HelloSessionBean"></soap:address><br>
</port><br>
</service><br>
</definitions>
Few points to notice:
-
A reasonable set of defaults are chosen for
portType/@name,
binding/@name,service/@nameand even thesoap:address/@location. Most of these values can be changed by specifying
a different value in the@WebServiceannotation. -
Accordingly to EJB 3.0 specification, if
@TransactionAttribute
is not specified on the method then a default value ofREQUIRED
is applied. This default value is automatically converted to
andATAlwaysCapabilityATAssertionpolicy assertions. -
Accordingly to
Web Services for Java EE, Version 1.2,webservices.xmlis
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
- Login or register to post comments
- Printer-friendly version
- arungupta's blog
- 879 reads





