我有一个 EJB Web 服务。我创建了由 Web 服务方法抛出的自定义故障类,效果很好。我的客户捕获了这些自定义异常并很好地处理它们。我遇到的问题是,如果 bean 的构造函数中发生异常并且我抛出自定义异常,则客户端不会收到我的自定义异常,它会收到以下内容:
javax.xml.ws.soap.SOAPFaultException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
at $Proxy34.getLots(Unknown Source)
我的问题是:可以在 EJB 的构造函数中抛出自定义异常并从客户端接收吗?如果是这样,我做错了什么。
我的代码:
EJB 构造函数
public LotManagement() throws LotManagementException
{
try {
pm = new PromisManager();
} catch (UnknownHostException ex) {
java.util.logging.Logger.getLogger(LotManagement.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
FaultBean LotManagementFaultBean = new FaultBean();
throw new LotManagementException(ex.getMessage() + "\n\n" + propMan.getProperty("ContactMessage"), LotManagementFaultBean, ex.getCause());
}
}
故障豆
package com.onsemi.cim.exception;
import javax.xml.ws.WebFault;
@WebFault(name="LotManagementException")
public class LotManagementException extends Exception {
private static final long serialVersionUID = 1L;
private FaultBean faultBean;
public LotManagementException() {
super();
}
public LotManagementException(String message, FaultBean faultBean,
Throwable cause) {
super(message, cause);
this.faultBean = faultBean;
}
public LotManagementException(String message, FaultBean faultBean) {
super(message);
this.faultBean = faultBean;
}
public FaultBean getFaultInfo() {
return faultBean;
}
}