如何在使用IBM Webexperience工厂创建的Web服务中抛出自定义故障代码和故障字符串?

时间:2015-09-02 10:09:26

标签: websphere websphere-portal

我在wef 8.0.0.2中创建了一个服务提供者模型,并使用服务定义构建器生成了wsdl。它有一个服务操作,它将验证用户,输入是用户名和密码。如果用户名无效,我想将错误显示为soap错误。因此,将在java类中抛出异常,该异常将验证用户名。抛出的肥皂故障如下所示

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>soapenv:Server.userException</faultcode>
            <faultstring>java.rmi.RemoteException: Error in method authenticate.  Error in method authenticateExecute.  Error in method authenticate_al.  Error in method authenticateUser_ljo.authorizeUser.  ||01013||Authentication Failed, Try again !</faultstring>
            <detail>
                <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">254</ns1:hostname>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

您可以看到故障字符串显示引发异常的跟踪。我只想展示||故障字符串中的分隔文本 - || 01013 ||验证失败,请重试!
抛出错误的ljo类也会显示

public class AuthenticateUser{
public void authorizeUser(WebAppAccess webAppAccess) throws Exception {
        try {
            String username = webAppAccess.getVariables().getString("userNameVar");
            String password = webAppAccess.getVariables().getString("passwordVar");
            if (username.trim().length() == 0 || password.trim().length() == 0) {
                throw new Exception();
            } else {
                System.out.println("Success");
            }
        } catch (Exception e) {
            throw new Exception("||01013||Authentication Failed, Try again !");
        }
    }
}

如何在抛出异常时设置自定义错误代码和错误字符串。??

1 个答案:

答案 0 :(得分:0)

请尝试使用自定义的例外,例如

public class AuthException extends Exception {

    public AuthException(String message, Throwable cause) {
        super(message, cause);
    }
}

并在web服务类的catch块中替换下面的行

抛出新的AuthException(&#34; || 01013 ||身份验证失败,再试一次!&#34;);

此致