Wednesday, February 23, 2011

JAX-WS with JBoss 6 Day 1 : Hello World

JBoss 6 ships with JAXWS 2.2 , while Java SE 6 has JAXWS 2.0 itself, so you need to copy all jar files under %JBOSS_HOME%/lib/endorsed to %JAVA_HOME%/jre/lib/endorsed.

1. Write a simple java class as your web service end point

package com.cn.i.easy.jboss.ws.ep;

import javax.jws.WebService;

@WebService
public class Hello {

public String sayHello() {
return "Hello by JBoss 6 WS with CXF implementation.";
}

}


2. Expose it as a Servlet (as EJB also can)



iJBossWS

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp


HelloService
com.cn.i.easy.jboss.ws.ep.Hello


HelloService
/ws/HelloService



3. After deploy your web application (war file) , your web service is up. And WSDL is available from jboss web service console : http://localhost:8080/jbossws/services.

4. To view generated WSDL : http://localhost:8080/iJBossWS/ws/HelloService?wsdl
























































5. By now your web service is done, and we can build web service client now with command : wsconsume -k -p com.cn.i.easy.jboss.ws.hello.client http://localhost:8080/iJBossWS/ws/HelloService?wsdl

6. Copy 6 generated files to source folder , then your project structure should as below


7. To make above files compiled change your maven pom.xml , jboss maven repository : https://repository.jboss.org/nexus/content/groups/public-jboss


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
i.easy
iJBossWS
war
0.0.1-SNAPSHOT



org.jboss.ws.cxf
jbossws-cxf-client
3.4.1.GA
provided


junit
junit
4.0
test




8. Let us write a test case as below

package test.com.cn.i.easy.jboss.ws.ep;

import static org.junit.Assert.assertEquals;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.junit.Before;
import org.junit.Test;

import com.cn.i.easy.jboss.ws.hello.client.Hello;
import com.cn.i.easy.jboss.ws.hello.client.HelloService;

public class TestHelloService {
private Client client = null;;
private Hello port = null;
private HelloService service = null;

@Before
public void setUp() {
service = new HelloService();
port = service.getHelloPort();
client = ClientProxy.getClient(port);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
}

@Test
public void testSayHello() throws Exception {
Object[] returnedObjects = client.invoke("sayHello", new Object[] {});
String expected = "Hello by JBoss 6 WS with CXF implementation.";
String result = (String) returnedObjects[0];
assertEquals(expected, result);
}

}


9. Download source code : http://www.mediafire.com/?3z1skepqo1s53o0

2 comments:

Bastille said...

Hi, I'm new to Java and Web Services. I was wondering if the complete WAR might be available?

Justin.Chi said...

Hi Bastille,

Thank you for the comments.

I have rewritten the post with source code provided, and I strongly suggest you building the war yourself as this should be the way to learn.

If you encounter any issue just let me know.