JavaEE 6: EJB Webservices in war
This is a new series of blogs which will cover what is new in JavaEE 6 with respect to WebServices
One of the new features of JavaEE 6 is ability to package ejb in a war. The EJB specification has always required that enterprise beans
be packaged in an enterprise module called an
Since it is common for Java EE web applications to use enterprise beans, this packaging requirement can be burdensome.
EJB 3.1 addresses this packaging complexity by removing the restriction that enterprise bean classes must be packaged in an
You can now place EJB classes directly in the
.war file, using the same packaging guidelines that apply to web application classes.
This means that you can place EJB classes under the
WEB-INF/classes directory or in a
.jar file within the
The EJB deployment descriptor is also optional. If you need it, you can package the EJB deployment descriptor as a
So we extend the above idea to demonstrate ejb webservices in a war file.
The following blog shows how simple it is to create and deploy ejb webservices in war using Netbeans 6.9 (http://netbeans.org) and Glassfish 3.0.1 (https://glassfish.dev.java.net/downloads/3.0.1-final.html)
Lets begin by creating a Web Application using Netbeans 6.9
Next we specify the project location
Select Glassfish 3.0.1 which is bundled with Netbeans 6.9 as the server. Note I use JavaEE 6 as the version .
Now we create a WebService say a Weather service which will show the temperature for a zip code. We implement the WebService as a Stateless Session Bean
Here is the code to the WebService . Note I have added a WebMethod. I have also specified an Interceptors annotation. EJB 3.0 interceptors provide developer
with fine grained control over method invocation flow. In this example we print the method from which the Interceptor was called.
Here is the code of SimpleInterceptor.java. This prints the name of the the method obtained from the InvocationContext.
Next we Clean and Build the project
Then click on Deploy.
You can browse the wsdl from here
Note (Ejb WebServices in war is merely a packaging convenience. The URL at which the endpoint is deployed is the same as if an ejb was deployed in a ejb-jar file. There was a bug in
Glassfish v3 where ejb webservices in war were being treated as webapps. This has been addressed in GF 3.0.1 and the GF 3.1)
So you see with Netbeans it is very easy to build, deploy ejb webservices in war. You can also test the webservice by
using the tester app at http://localhost:8080/WeatherService/Weather?tester and enter a zip code (Ofcourse you will get the hardcoded 70 degress)