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;
import javax.xml.ws.WebFault;

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

@WebFault
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.

1 comment:

mahakk01 said...

In this post you can see faults in JAX WS. We sometimes face faults when we work. The code is not easily understandable by the beginners. Only experts can get the concept with this syntax. Thanks for the information given in this post. Keep it up.
sap support packs