Skip to main content

JavaEE 6: EJB Webservices in war

Posted by bhaktimehta on June 25, 2010 at 2:42 PM PDT

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 ejb-jar file.
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 ejb-jar file.
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 WEB-INF/lib directory.
The EJB deployment descriptor is also optional. If you need it, you can package the EJB deployment descriptor as a WEB-INF/ejb-jar.xml file. 

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

choose project

 Next we specify the project location

name and location

 

 Select Glassfish 3.0.1 which is bundled with Netbeans 6.9 as the server. Note I use JavaEE 6 as the version .

server and settings

 

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

name and loc

 

Click Finish

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.

 

code

 

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

http://localhost:8080/WeatherService/Weather?wsdl

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)

wsdl

 

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)

Additional Information

Java EE 6 technologies

EJB 3.1 specification

JSR 109 specification

 

Comments

<p>I have an ejb web service in a war. &nbsp;The web service ...

I have an ejb web service in a war. The web service class has a @RolesAllowed annotation. As the web service is not treated as a webapp any more, how can I execute the service from a Java Client?. If I execute the following method in the client:
private static void setCredentials( UserManager eif ) {
Map<String, Object> context = ((BindingProvider) eif).getRequestContext();
context.put( BindingProvider.USERNAME_PROPERTY, "tracy");
context.put( BindingProvider.PASSWORD_PROPERTY, "tracy");
}
There is no role associated to the call, and the web service container refuses to execute the web service.
Regards,

GF 3.1 has problem with Stateless SessionBeans as WebService

Hi, I had a requirement to use WELD >=1.1.0Beta1 to avoid a WELD bug related to overridden methods with covariant return types. So I had upgraded to GlassFish 3.1-b19 and found that SLSBs annotated with @WebService in war package are causing an exception in org.glassfish.ejb.startup.EjbDeployer load method. After A long analysis I found the issue and removed all javax.jws.* annotations from those SLSBs to get rid of the problem. Any Idea? What could be the reason? Is this just another bug with GlassFish or Somehow the profile that GF is using doesn't contain support for Web-service Regards, Puspendu Banerjee

GF 3.1 has a problem with Stateless SessionBeans as Webservice

This seems to be a bug caused by some recent changes. WIll look into this and post an update soon

bhaktimehta wrote: This seems

bhaktimehta wrote:

This seems to be a bug caused by some recent changes. WIll look into this and post an update soon

Any update on this? I'm using GlassFish Server Open Source Edition 3.1-b34 (build 34) and am deploying a WS inside a WAR. It's working just fine, however when I view the application inside the GF Admin Console, it doesn't recognize my WS as a WS, its listed as a StatelessSessionBean and there is no "View Endpoint" link.
I can view the wsdl by manually entering the url and clients are able to connect to the WS, so it seems to be an issue with the GF Admin Console.
Thanks,
-sellerjd