gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:46:04 25 4
gpt4 key购买 nike

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

}

响应信息类:

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 类中完成)。

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

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

关于java - 无法创建 jaxb 上下文 - WebFault 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com