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

时间:2015-03-18 07:25:17

标签: java web-services jaxb

方法initiateBatchProcess出错:必须捕获或声明要抛出未报告的异常。我有一个带有@webFault批注的自定义异常类来处理jaxb异常,如link所示。

我的initiateBatchProcess()方法已经扩展了自定义异常类,但仍显示错误

@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();
      }

自定义异常类:

@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类:

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;
    }

}

ResponseInfo类:

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;
    }   
}

1 个答案:

答案 0 :(得分:1)

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

您必须声明WT.initiateBatchProcess()抛出Exception或抓住Exception并将其重新抛出为MyServiceException(可以在ResponseInfo或WT类中完成)。

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

@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)构造函数,但您可以根据需要修改代码。