Monday, July 6, 2009

Faults in JAX WS

Continuing with last post, we would look in to a scenario where in our java program is throwing an exception, which is fairly common scene in any service. So to look at this scenario, we would  would make a small change to our Java program to enable our web service to return fault on occurence of certain event.

Except of the code all other steps would remain same as they were in Deploy JAX-WS on Tomcat through Eclipse.

The code now incorporates

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

public class SwapException {
 public String greetException(@WebParam String myName)  
  throws MyException{
  if(myName.equals("Swap") || myName == null) {
  throw new MyException();
  else {
  return "Hello "+myName;

class MyException extends RuntimeException {
 private static final long serialVersionUID = 5476051236444095955L;
 public MyException() {
  super("Exception by Swapnil, don't say swap");

And follow the rest of the procedure to expose it as web service, as discussed in earlier post. You can even create a client and check how it behaves.

Friday, July 3, 2009

Deploy JAX-WS on Tomcat through Eclipse

Today, I am going to present the step-by-step process to create JAX-WS compliant web service using eclipse and deploy it on tomcat web server. 

Scope of this post is limited to create a JAX-WS webservice and does not discuss JAX WS implementation as such.

You would require to download the binary and execute this jar as

 java -jar JAXWS2.1.1_20070501.jar

This would create a directory called jaxws-ri, henceforth refered to as %JAXWS_HOME%.

Configuring Eclipse

In Eclipse right-click to create a new "Dynamic Web Project", lets name it JAXWSCalculator.Now right click "Java Resources:src" and create a new class, say, Calculator in package de.swapgmbh.service.calculate. Now provide the code below with annotations -

package de.swapgmbh.service.calculate;

import javax.jws.WebMethod;
import javax.jws.WebService;
public class Calculator {
public int add(int expr1, int expr2) {
System.out.println("Now adding " + expr1 + " and "+expr2);
return expr1+expr2;

Now we need to create wsgen tool to process the class and generate web service. The wsgen.bat file is located in %JAXWS_HOME%\bin directory.

From the menu select Run > External Tools > External Tools...

This would open up a new window wherein you need to configure the wsgen tool.

Provide the following details >


In the Main Tab>

Location:D:\jaxws-ri\bin\wsgen.bat  (%JAXWS_HOME%\bin\wsgen.bat)


-verbose -wsdl -keep -r "${project_loc}\WebContent\WEB-INF\wsdl" -d "${project_loc}\generated" -cp D:\Work\SwapStudy\workspace\JAXWSCalculator\build\classes ${java_type_name}

-r tells the wsgen where to keep the generated wsdl file.
-d tells the wsgen where to keep the generaed output files.

In the Environment Tab>

Create two variable

JAVA_HOME  - C:\jdk1.5.04
JAXWS_HOME - D:\jaxws-ri

Now click Apply and Close.

Now in your project create "wsdl" folder in "WEB-INF" and a folder name "generated" on the root of your project. 

Nowhighlight the Calculator class and Run the "wsgen". This would generate the wsdl and schema file in wsdl folder and other required classes in "generated" folder.

Now we need to modify the web.xml and create sun-jaxws.xml. Open the web.xml, and place the following details in.

<web-app id="WebApp_ID" version="2.4" xmlns="" xsi="" schemalocation="">
<description>JAX-WS endpoint - calculator</description>


Now put in the following details in sun-jaxws.xml, which would be located in \WEB-INF folder, beside web.xml file.

<endpoints xmlns="" version="2.0">
<endpoint name="calculator" implementation="de.swapgmbh.service.calculate.Calculator" pattern="/addnumbers">

Now, right click the project and click "Export >WAR File", place the war file at a preferred location.

Now copy the war file, place in webapps folder of your tomcat, and now you could access the wsdl file using "http://localhost:8080/JAXWSCalculator/addnumbers?wsdl"

You may want to use SOAP UI to access the service and see it working.