I am trying to expose one method as webservice from backend using JAX-WS with Tomcat. The backend method is something like below (in CompanyFacade class):
public Company findCompanyById(Long id) throws Exception{
Company c = null;
try{
if(id == null){
throw new Exception("Failed to get company ID");
}
c = baseFacade.load(Company.class, id);
if(c == null || c.getId() == null){
throw new Exception("No company found");
}
}
catch(Exception e){
throw new Exception(e.getMessage());
}
finally{
return c;
}
}
As for the webservice class, there's a WebMethod invokes the above method:
@WebMethod(operationName = "findCompanyById", action = "urn:findCompanyById")
@WebResult(name = "Company")
public Company findCompanyById(@WebParam(name = "id") Long id) throws Exception{
return CompanyFacade.findCompanyById(id);
}
Below is the respond message I got, which is supposed to have the exception message:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findCompanyByIdResponse xmlns:ns2="http://site.com/api/ws"/>
</S:Body>
</S:Envelope>
The webservice works fine, the only problem is about the exception (e.g. No company found), the exception messages can be displayed in the Tomcat console, but not in the SOAP response message, it seems that the invoked method in the WebMethod doesn't return any exception. So the question is: how to parse the exception message from the method in backend to the SOAP response message OR is there any other design pattern? Thanks in advance!