Engineering Full Stack Apps with Java and JavaScript
Java application exceptions can be mapped to SOAP faults in WSDL/SOAP message.
We will create a simple web service class with a single method that will accept an integer and check if it is even. If even we will return “Received even N” where N is the received even number.
If received number is not even, then we will throw a custom exception NotEvenNumberException that extends Exception.
We will publish the web service along with the custom exception into Glassfish applicaiton server.
We will however test using a standard testing tool such as SOAP UI. This is because Glassfish server web service tester will not show the SOAP message as is, in case of a SOAP fault; instead it will show you the complete stacktrace.
package com.javajee.webservices.soap;
import javax.jws.WebService;
@WebService
public class EvenNumberChecker {
public String onlyEven(int even) throws NotEvenNumberException {
if (even % 2 == 0) {
return "Received even" + even;
} else {
throw new NotEvenNumberException(1);
}
}
}
package com.javajee.webservices.soap;
public class NotEvenNumberException extends Exception {
public NotEvenNumberException(int i) {
super("Not an even numbber:"+i);
}
private static final long serialVersionUID = 1L;
}
Once the service is deployed and Glassfish is restarted, you can verify the WSDL file using the below url:
http://localhost:8080/WebServiceDemo/EvenNumberCheckerService?wsdl
Note: You may also get the wsdl url from your Glassfish Admin console.
SOAP Request and SOAP response
Test using a standard testing tool such as SOAP UI.
Glassfish server web service tester will not show the SOAP message as is, in case of a SOAP fault; instead it will show you the complete stacktrace.
If the exception is a checked exception, the corresponding fault details are present in the wsdl. However, if the exception is a unchecked exception (RuntimeException or its subclasses), the corresponding fault details are present in the wsdl. In both cases, the SOAP response will have fault details if an exception is thrown.
Comments
Glassfish Server does not
Glassfish Server does not display the SOAP Fault messages. It is mainly used only for Dev to identify the exception. There are tools available where you can see the exact SOAP Response or Fault message and one such open source tool is "Soap UI Client". Try downloading the SOAP UI Plugin and use it to see the SOAP Fault message.