无法创建 jaxb 上下文 - WebFault 错误

Unable to create jaxb context - WebFault error

方法 initiateBatchProcess 出错:必须捕获或声明要抛出未报告的异常。我有一个带有 @webFault 注释的自定义异常 class 来处理此 link 中建议的 jaxb 异常。

我的 initiateBatchProcess() 方法已经在扩展自定义异常 class 但它仍然显示错误

@WebService(serviceName = "WT_WebService")
public class WT
{    
    ResponseInfo result = new ResponseInfo();

    @WebMethod(operationName = "initiateBatchProcess")
      public @WebResult(name = "Response") ArrayList initiateBatchProcess(@WebParam (name = "BatchID")int BatchId, @WebParam (name = "MPTRef")String MPTRef) throws MyServiceException
      {
          return result.initiateBatchProcess();
      }

自定义异常 class:

@WebFault(name="MyServiceException", faultBean = "com.ws.MyServiceFault", targetNamespace="http://wataniya.com/")
public class MyServiceException extends Exception {

    private static final long serialVersionUID = 1L;

    private MyServiceFault faultBean;

    public MyServiceException() {
        super();
    }

    public MyServiceException(String message, MyServiceFault faultBean, Throwable cause) {
        super(message, cause);
        this.faultBean = faultBean;
    }

    public MyServiceException(String message, MyServiceFault faultBean) {
        super(message);
        this.faultBean = faultBean;
    }

    public MyServiceFault getFaultInfo() {
        return faultBean;
    }
}

FaultBean class:

public class MyServiceFault {
    /**
     * Fault Code
     */
     private String faultCode;
     /**
      * Fault String
      */
     private String faultString;
    /**
     * @return the faultCode
     */
    public String getFaultCode() {
        return faultCode;
    }
    /**
     * @param faultCode the faultCode to set
     */
    public void setFaultCode(String faultCode) {
        this.faultCode = faultCode;
    }
    /**
     * @return the faultString
     */
    public String getFaultString() {
        return faultString;
    }
    /**
     * @param faultString the faultString to set
     */
    public void setFaultString(String faultString) {
        this.faultString = faultString;
    }

}

响应信息Class:

public class ResponseInfo
{    
    static Properties props = new Properties();

    public ArrayList initiateBatchProcess() throws Exception
    {
        ArrayList list  = new ArrayList();
        props.load(ResponseInfo.class.getResourceAsStream("ResponseFields.properties"));
        String method1_status = props.getProperty("method1_status");
        String method1_comments = props.getProperty("method1_comments");
        list.add(method1_status);
        list.add(method1_comments);
        return list;
    }   
}

ResponseInfo.initiateBatchProcess() 声明抛出已检查的异常 Exception 但未在 WT.initiateBatchProcess().

中处理

您必须声明 WT.initiateBatchProcess() 以抛出 Exception 或捕获 Exception 并将其作为 MyServiceException 重新抛出(这可以在 ResponseInfo 或 WT class).

编辑:第二个选项实现如下所示:

@WebMethod(operationName = "initiateBatchProcess")
  public @WebResult(name = "Response") ArrayList initiateBatchProcess(@WebParam (name = "BatchID")int BatchId, @WebParam (name = "MPTRef")String MPTRef) throws MyServiceException
  {
      ArrayList returnValue = null;
      try {
          returnValue = result.initiateBatchProcess();
      } catch (Exception e) {
          throw new MyServiceException(e);
      }

      return returnValue;
  }

为了简单起见,我添加了一个 MyServiceException(Exception e) 构造函数,但您可以根据需要修改代码。