This blog is mainly about Java...

Wednesday, February 4, 2009

How to create and use a WebService with Axis 2 and Seam 2.x in JBoss 4.x

In this example, I will show how you can create a Webservice using Axis 2.
First of all, download the latest version of Axix 2 from

To create a WebService in Java EE 5 you can use the annotation @WebService.
We also annotate this class as a seam component so that we can incorporate it in our existing business logic.

This is our WebService:

package somepackage.webservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.EntityManager;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

@WebService(name = "FooService", serviceName = "FooService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class FooService implements FooServiceLocal {

@In EntityManager entityManager;

@In Credentials credentials;

private boolean login(String username, String password) {
return Identity.instance().isLoggedIn();

private boolean logout() {
return !Identity.instance().isLoggedIn();

public List<FooCanonical> getFoo(@WebParam(name = "username")
String username, @WebParam(name = "password")
String password, @WebParam(name = "orgnumber")
String orgnumber) {
// orgnumber can be null!
if (username == null || password == null) {
return null;
//First thing we do is to login to ensure that the user has the correct username/password
//We are using basic seam login method
boolean isLoggedIn = login(username, password);

if (isLoggedIn) {

List<FooCanonical> returnList = new ArrayList<FooCanonical>();
//Do some stuff with the list
//Remember to log out
return returnList;
} else {
// Probably wrong username password
return null;


Next what we need to do, is create a way for this webservice to interact with JBoss through our SOAP definition. We do that by creating a xml file called

<jaxws-config xmlns="urn:jboss:jaxws-config:2.0"
xsi:schemaLocation="urn:jboss:jaxws-config:2.0 jaxws-config_2.1.xsd">
<config-name>Seam WebService Endpoint</config-name>
<javaee:handler-name>SOAP Request Handler</javaee:handler-name>

And place this file in the $JBOSS_HOME/resources/META-INF directory.
Now you are done! Deploy your application and look in
and see if your WebService is correctly deployed and the wsdl available.
This should look something like this:

Endpoint Addresshttp://localhost:8080/foo-foo/FooService?wsdl

Next, we will use the Axis2 framework to create client stubs by using axis2-1.4.1 and the script wsdl2java. Navigate to $AXIS_HOME/bin and type in the following command:
./ -uri -o build/client

This command will create an ant script under the directly build/client.
Now go to build/client and type ant after setting $AXIS_HOME. This will generate FooService-test-client.jar which we now can use to retrieve data from the WebService in the client. I recommend changing the name to something more appropriate.

In your client, you can call the getFoo WebMethod like this:

FooServiceStub stub;
GetFoo getFoo;

stub = new FooServiceStub();
getFoo = new FooServiceStub.GetFoo();

FooServiceStub.GetFooE fooImpl = new FooServiceStub.GetFooE();

//Retrieve the List as an array
FooCanonical[] get_return = stub.getFoo(fooImpl).getGetFooResponse().get_return();
//Do what you want with the array

Note that even if you return a List from the WebService, you will get it as an array. But it is quite easy to put it in a List in the client afterwards. Also remember that the username and password is sendt in clear text, so you might want to send it through https, so it is encrypted.


Anonymous said...

Do you know if it is possible to create a seam webserive in a non-ejb environment (WAR) using JBoss AS.



Shervin Asgari said...

Sure. Its not different from normal webservices.

Have a look here:

Asker Ali M said...

I was trying to implement the same with JBoss 5 ans Seam 2.2, with following details
Web service as follows

@WebService(name = "SubmittedTimesheetService", serviceName = "SubmittedTimesheetService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class SubmittedTimesheet {
public String calculateSomething(@WebParam(name = "username")
String username, @WebParam(name = "password")
String password) {
return username + password;

With respect to some another article i implemented Servlet inside web.xml as follows



Now I am getting the proper WSDL file on URL localhost:8080/CGITimesheet/Timesheet?WSDL.

Now I have created the Stub using AXIS2 1.6 version.

The Issue I am facing is while I am running the client, I am getting the following error.
org.apache.axis2.AxisFault: First Element must contain the local name, Envelope , but found html
at org.apache.axis2.AxisFault.makeFault(
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(
at org.apache.axis2.description.OutInAxisOperationClient.send(
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(
at org.apache.axis2.client.OperationClient.execute(
at org.cgi.timesheet.model.SubmittedTimesheetServiceStub.calculateSomething(
at org.cgi.timesheet.model.CallWS.main(
Caused by: org.apache.axiom.soap.SOAPProcessingException: First Element must contain the local name, Envelope , but found html
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.constructNode(
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createOMElement(
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createNextOMElement(
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.(
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.(
at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(
at org.apache.axis2.transport.TransportUtils.createDocumentElement(
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(

Please help me on the same.

Note :
I havent created standard-jaxws-endpoint-config.xml file , But my WSDL is working fine.

Shervin Asgari said...

This is really not the place for me to help you, on my blog. However it looks like Axis gets html instead xml.

I suggest you use a forum to get help on your issue, either or Axis user forum.


özge said...

I have a web service login method like that:
public boolean login(String username, String password)
System.out.println("logine gelen username: "+username);
System.out.println("logine gelen password: "+password);
System.out.println("Identity.instance().getCredentials().getUsername() "+Identity.instance().getCredentials().getUsername());
System.out.println("Identity.instance().getCredentials().getPassword() "+Identity.instance().getCredentials().getPassword());
System.out.println("Identity.instance() "+Identity.instance());
System.out.println("Identity.instance().getUsername() "+Identity.instance().getUsername());
System.out.println("Identity.instance().isLoggedIn(): "+Identity.instance().isLoggedIn());
if (Identity.instance().isLoggedIn()==true)
return true;
return false;

but i cannot login because Identity.instance().login(); does not work for me. Do you have any suggestions?


Shervin Asgari said...

The code should work. What is the error message, if any? Do you know that normal authentication works?

özge said...

I do not have an error message,I just start the jboss server successfully and when I test client side of the project,
System.out.println("Identity.instance().isLoggedIn(): "+Identity.instance().isLoggedIn());

gives me false value.
I use seam authentication, in components.xml I have

Thank you for reply..

Shervin Asgari said...

Then its most likely a problem with your seam setup.

What you should do is create the seam project using seam-gen. It ensures you get everything correctly, and it is quite fast and easy to setup.

Read more about it in the documentation

tom said...

Is it possible to say more about creating the client? If I use your code, the classes will not compile because referenced classes are not available. Are there axis classes which we have to include in our deployed jars and classpath?

Shervin Asgari said...

I am not sure what you mean. But you generate clients using this command: ./ -uri -o build/client

This will create a client for you in the build/client directory from where you type the command.