10 April 2007 - 1.7 home user-guide eclipse jbossws intellij netbeans maven 1.X/2.X PDF files forums bugs sourceforge






Vote for soapUI at the WSJ Readers' Choice awards in the

'Best Web Services Utility' and

'Best Web Services Testing Tool'

categories

Publish EJB3 as Web Service with Annotations - Walkthrough Example

The following example gives a complete walkthrough of how to publish a POJO both as an EJB3 stateless session bean and Web Service. The development environment is as follows:

Start by installing the above and setting the path to the wstools script as described in the Overview.

Create the project

Create a standard empty java project;

Add a source folder for src/java and create a jbosstest package.

Create Remote Interface and EJB3

Create a remote interface:

package jbosstest;

public interface HelloWorldRemoteInterface {

	public String sayHello(String subject) ;
}

And the EJB:

package jbosstest;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.jboss.annotation.ejb.RemoteBinding;

@Stateless
@Remote(HelloWorldRemoteInterface.class)
@RemoteBinding(jndiBinding = "/ejb3/EJB3EndpointInterface")
public class HelloWorld implements HelloWorldRemoteInterface 
{
	public String sayHello(String subject) 
	{
		return "Hello " + subject + "!";
	}
}

(You will need to add jboss-ejbx.jar and jboss-ejb3-client.jar to your project classpath)

Now enable the JBossWS Nature as described in Getting Started. Right-click the EJB3-class and select "JBossWS / Add Web Service Annotations", which will show the following dialog:

Change the Binding Style to "rpc" and select Generate. The EJB will be annotated as follows:

package jbosstest;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.jboss.annotation.ejb.RemoteBinding;

@javax.jws.soap.SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.RPC)
@javax.jws.WebService(name = "HelloWorld", targetNamespace = "urn:jbosstest", serviceName = "HelloWorldService")
@Stateless
@Remote(HelloWorldRemoteInterface.class)
@RemoteBinding(jndiBinding = "/ejb3/EJB3EndpointInterface")
public class HelloWorld implements HelloWorldRemoteInterface {
	@javax.jws.WebMethod()
	public String sayHello(String subject) {
		return "Hello " + subject + "!";
	}
}

(You can always rerun the dialog for modifying/removing annotations)

Package and Deploy

Packaging must currently be performed manually: right-click the project and select "Properties". In the properties dialog select "Packaging Configurations":

Enable packaging and create a new jar package containing the projects output folder (as shown above).

After closing the dialog, right-click the project and select "Run Packaging", this will create the configured .jar file and log to the console:

Now right-click the generated jar and select "Run as / Run on Server" and select your configured JBoss 4.0.4 instance. The jar file will be deployed and the following should be logged to the jboss console:

Test

Since no WSDL was generated in the project, the published Web Service has not yet been imported into the JBossWS node. Right-click the JBossWS Web Services node (in the Project Explorer View) and select "JBossWS / Add WSDL from URL". In the prompting dialog, specify "http://localhost:8080/HelloWorld/HelloWorld?wsdl" (taken from the deployment log with an appended "?WSDL"). The WSDL will be imported into JBossWS node and is now available for invocation;

Expand the JBossWS nodes children and double-click the "Request 1" node which will open the request editor:

Change the '?' to 'Marc' and submit the request using the green arrow in the toolbar. A response will be shown in a second editor tab:


Next: Back to Overview