I have developed a webservice using Apache CXF ,which will be in production very soon .
I am concerned about the exception handling in this , i am not sure whether what i followed is correct or not .
I have a method shown below which i exposed i as a webservice
import javax.jws.WebService;
@WebService
public interface TataWebService {
public String distragery()throws Exception;
}
public String distrager throws Exception {
int a = 30;
strategyData = "currentlyhadcoced" ;
if(a==30) {
throw new IncorrectProjectIdsException("The Value of a is 30");
}
return strategyData;
}
And the way i defined User defined exception is this way
@WebFault(name = "IncorrectProjectIdsDetails")
public class IncorrectProjectIdsException extends Exception {
private java.lang.String incorrectProjectIdsDetails;
public IncorrectProjectIdsException (String message) {
super(message);
}
public java.lang.String getFaultInfo() {
return this.incorrectProjectIdsDetails;
}
}
Please tell me if this is correct , regarding the throws declaration inside the method signature or shuld we handle in any other manner ??
Thank you very much
You should throw the specific subclass of
Exceptionin your interface that’s annotated with@WebServiceso that the JAX-WS engine knows to publish the information about which faults are possible. That’s because that is information that is discovered statically by examining the declarations, not by dynamic discovery of the exceptions that are actually thrown.If you’re stuck with a lower-level API that can throw anything (it happens; indeed, it happens a lot) then you should wrap that lower-level exception. Here’s a simple way of doing that:
Which you’d use like this inside your web method:
(There are other ways of doing the exception mapping, but they’re quite a lot more complex. Wrapping is at least easy.)