An EJB3 WebService with Generated Client Code

This is an example of an EJB3 webservice. I am using jboss-5.0.1.GA.
My workspace has the following structure:


     

     
          
               
                    TestEjbWS.java
                    
                         TestEjbAPI.java


     javaee.jar
     javax.jws-1.0.jar

     application.xml
     jboss-app.xml
build.xml

The ejb class that is exposed as a web service looks like this:


package com.mattiz.ejb.ws;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.mattiz.ejb.ws.api.TestEjbAPI;

@Stateless(name = "TestEjbWS", mappedName = "ejb/TestEjbWS")
@Remote(TestEjbAPI.class)
@WebService(serviceName = "TestEjbWS", portName = "TestEjbWSPort", targetNamespace = "http://mattiz.com/test/testejbws")
public class TestEjbWS implements TestEjbAPI {
	@WebMethod(operationName = "testMe", action = "urn:testMe")
	public String testMe() {
		return "Hallo World!!";
	}
}

The interface that this ejb implements looks like this:

package com.mattiz.ejb.ws.api;

import java.rmi.Remote;

public interface TestEjbAPI extends Remote {
	public String testMe();
}

contains two jars required for compiling the app and is NOT bundled with the ear.

application.xml looks like this



 My Application
 
 mattiz-test.jar
 
 lib

jboss-app.xml looks like this:




 
 mattiz:archive=mattiz-test.ear


The build file for creating the ear is reproduced below:



    
    
        
            
        
    
    
        
        
        
        
        
        
    
    
        
    
    
        
        
    
    
        
            
                
            
            
        
    

The ejbs are packaged in a jar and wrapped in a ear.
The ear file is generated and located in the

dist

folder.

Once the generated ear is deployed in jboss, verification of proper deployment is done by typing the following url in the browser:
http://localhost:8080/mattiz-test/TestEjbWS?WSDL
This will display the wsdl file for this web service.

Now for generating the client for this web service.

I have another application to generate the web service client stubs with the following folder structure:



     

          
               
                    
                         MattizWSClientTest.java
                         
                              
                              

     jaxb-impl-2.1.8.jar
     jaxb-xjc-2.1.6.jar
     jaxws-rt-2.1.4.jar
     jaxws-tools-2.1.4.jar
     saaj-impl.jar
     stax-ex-1.2.jar
     streambuffer-0.8.jar
build-client.xml

MattizWSClientTest.java looks like this:

package com.mattiz.ws.client;

import com.mattiz.ws.test.bean.TestEjbWS;
import com.mattiz.ws.test.bean.TestEjbWS_Service;

public class MattizWSClientTest {
	public static void main(String[] args) {
		try {
			TestEjbWS port = new TestEjbWS_Service().getTestEjbWSPort();
			System.out.println(port.testMe());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

TestEjbWS_Service is not yet generated so the class would give a compile time error.

the build xml for generating the client stubs is reproduced below:



	
		
		
			
		
	
	
		
	
	
		
		
		
		
		
	
	
		
	
	
		
			
		
	
	
		
		
	

The generated stubs have the following package structure:

com.mattiz.ws.test.bean

Place the generated client jar, mattiz-test-client.jar, on the classpath of MattizWSClientTest and run it as a standalone.
You would see the following message on the console:
Hallo World!!

Please note that the java6 version you have installed on your system may give a problem while generating the client from the WSDL.
To solve this problem you might need to point JAVA_HOME to a newer installation that might be got here->Java SE 6 Update 4.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: