【问题标题】:Handling Exception custom in JAX-WS在 JAX-WS 中处理异常自定义
【发布时间】:2013-08-05 22:08:27
【问题描述】:

我有使用 JAX-WS 开发的 Web 服务。现在我想在某些条件下使用自定义错误代码抛出 SOAPFault。

我有一个网络故障:

@WebFault(name = "BankExceptionFault1_Fault", targetNamespace = NS.namespace)

公共类 BankException 扩展异常 {

private WebMethodStatus faultInfo;


public BankException(Errors error) {
    this(error, error.name());
}

public WebMethodStatus getFaultInfo() {
    return faultInfo;
}

public BankException(Errors error, String description) {
    super(error.getErrorCode());
    this.faultInfo = new WebMethodStatus(error, description);
}

}

并且在某些方法中,对于给定的条件,抛出异常:

@Override
@WebMethod(operationName = "UpdateAccountRecord")
@WebResult(name = "Result")
@LogExecution
public WebMethodStatus updateAccountRecord(
        @WebParam(name = "Request") UpdateAccountRequest request) throws BankException {
    if (!Boolean.parseBoolean(specialMode)) {
        throw new BankException(Errors.INVALID_RUNNING_MODE,
                "Can't update account record. For updating need special running mode");
    }
    service.updateAccountRecord(request);
    return new WebMethodSuccessStatus();
}

在 spring-mvc 应用程序中,我想捕获我的异常:

try {
        wsPort.updateAccountRecord(updateAccountRequest);
    } catch (BankException e) {
        throwException(e);
    }
    catch(RemoteAccessException e){
        throwException(e);
    }

但如果尝试使用 sring-mvc 应用程序更新帐户,总是返回 RemoteAccessException。 detailMessage:无法在 [http://localhost:8080/my-app-2.1.1-SNAPSHOT/app/MyApp] 访问远程服务 原因:java.lang.IllegalStateException:当前事件不是 START_ELEMENT 或 END_ELEMENT

但如果我使用soapui 更新帐户,返回正确的异常:

BNK00017 无法更新帐户记录。对于更新需要特殊的运行模式

【问题讨论】:

    标签: java spring spring-mvc error-handling jax-ws


    【解决方案1】:

    如果wsPort 类似于注入的JaxWsPortProxyFactoryBean,那么您的异常很可能被RemoteAccessException 包装。尝试使用RemoteAccessException.getCause() 看看你会得到什么......

    【讨论】:

    • 这是一个结果:javax.xml.ws.WebServiceException: java.lang.IllegalStateException: Current event not START_ELEMENT or END_ELEMENT
    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2010-12-09
    相关资源
    最近更新 更多