【问题标题】:Unable to create jaxb context - WebFault error无法创建 jaxb 上下文 - WebFault 错误
【发布时间】:2015-05-20 21:29:30
【问题描述】:

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

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

【问题讨论】:

  • ResponseInfo.initiateBatchProcess() 方法呢,除了 MyServiceException 之外,它会抛出任何其他异常吗?
  • 它只抛出未报告的异常
  • 能否提供 ResponseInfo 类的来源,因为到目前为止我没有看到代码有任何问题?
  • @SergeyPauk 更新了问题

标签: java web-services jaxb


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

【讨论】:

  • 如何为MyServiceException(e) 创建一个构造函数,因为它给了我错误。
  • 这是我用过的:public MyServiceException(Exception e) { super(e); }
  • 当然你可以使用MyServiceException(String message, MyServiceFault faultBean, Throwable cause)构造函数。在这种情况下,您需要创建并填充 faultBean
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2021-04-08
  • 1970-01-01
  • 2011-11-16
  • 2011-04-27
  • 1970-01-01
相关资源
最近更新 更多