【问题标题】:Custom exceptions handling - java Web Services自定义异常处理 - java Web 服务
【发布时间】:2011-12-06 13:54:57
【问题描述】:

这是我第一次在这里发帖,所以请耐心等待,并在必要时纠正我......

我正在使用带有 GlassFish 的 NetBeans 构建简单的基于 Web 服务的应用程序。 NetBeans 在为新的 Web 服务及其操作生成代码方面确实有很大帮助,但有一件事让我抓狂——Web 服务异常处理。操作如:

@WebMethod(operationName = "checkUserExist")
public String checkUserExist(@WebParam(name = "login") String login, @WebParam(name = "password") String password) throws LoginException {

    String auth_code = "";
    bk_end.Validate val = new bk_end.Validate();

    boolean isValidated = val.check(login, password);

    if(isValidated)
    {
        auth_code = val.return_AuthCode();
        bank_services.CustomerSerice.setAuth_Code(auth_code);
        return auth_code;
    }

    throw new LoginException("Something is wrong!");

}

以及异常类:

public class LoginException extends Exception
{
    String message;


    public LoginException(String message)
    {
        super();
        this.message = message;
    }

    public String getErrorMessage()
    {
        return this.message;
    }
}

抛出一个巨大的 异常详情:java.lang.reflect.InvocationTargetException 加上大量其他例外.. 我意识到这是一个非常愚蠢的问题,但是在尝试了很多小时后,我只是不知道该怎么做。 我已经阅读了有关 @WebFault 的内容,但不知道如何正确指定它(将我的异常附加到特定方法..)

请帮忙,我们欢迎所有想法!

我遇到的例外情况: 服务调用抛出异常消息:null;有关详细信息,请参阅服务器日志

Exceptions details : java.lang.reflect.InvocationTargetException
javax.servlet.ServletException: java.lang.reflect.InvocationTargetException
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:330)
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:106)
    at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:114)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.doFilter(ServletAdapter.java:1002)
    at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.invokeFilterChain(ServletAdapter.java:942) 
    at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:404)
    ...
Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:301)
    ... 24 more
Caused by: bank_services.LoginException_Exception: excepts.LoginException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:145)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:123)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:144)
    at $Proxy383.checkUserExist(Unknown Source) ... 29 more             

【问题讨论】:

  • 你说当你抛出 LoginException 时,你会得到“几十个其他异常,但我想要的那个”。您能否向我们展示一下引发了哪些异常?
  • 没错,用所需的详细信息更新了我的问题!
  • 有人...?我已经一整天了,必须有解决方案,这是一个初学者的问题......
  • 你“想要”什么例外?我不明白他的问题是什么。 InvocationTargetException的原因应该是导致问题的异常。
  • 好的,我上面的错误 - 不是我“想要”任何异常,而是我想要实现的是能够(在客户端)获得“出现问题”异常消息当被抓住时......

标签: java exception soap service web


【解决方案1】:

哦,好的。您想看到“出现问题”消息。

所以我认为这里的最终问题是您没有在Throwable 中使用标准的detailMessage 字段。如果您只是将消息传递给基类 (super(message);),那么我敢打赌您会在跟踪中看到异常。您是否尝试过其他 Exception 类型,例如 Exception

您还可以将LoginException.toString() 定义为:

@Override
public String toString() {
    String s = getClass().getName();
    return (message != null) ? (s + ": " + message) : s;
}

或者,您需要执行以下操作:

try {
    ...
} catch (Exception e) {
    if (e instanceof ServletException) {
        e = e.getCause();
    }
    if (e instanceof InvocationTargetException) {
        e = e.getCause();
    }
    if (e instanceof LoginException) {
        System.err.println("Login exception returned message: "
            + ((LoginException)e). getErrorMessage());
    } else {
        System.err.println("Exception returned message: " + e);
    }
}

但我认为我的建议是在您的构造函数中使用super(message);

【讨论】:

  • 正如我已经提到的,我的网络服务知识非常基础......我想要实现的目标:我的客户端应用程序发送两个字符串,网络服务操作会检查它们,如果正确则很好,如果不正确-> 抛出登录异常。如果在服务器端抛出(并且工作正常)消息(“出现问题”)将显示给尝试登录的用户......就是这样......
  • 客户端代码有一个最新的LoginException 类的副本?你应该在你的异常中添加一个serialVersionUID 字段并用一个数字更新它。如果您的客户端没有正确版本的LoginException,那么它将引发异常。
  • "客户端有这个类吗?" - 不确定是否理解正确 - 我正在使用 NetBeans 生成客户端-服务器相关代码...
  • 您是否尝试设置标准消息?将您的构造函数更改为只调用super(message); 怎么样?你能打印出exception.getCause().getCause().getErrorMessage()吗?
  • 我的机器刚刚死在我身上,最重要的是……明天继续 - 格雷 -> 谢谢你的投入!
猜你喜欢
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 2011-03-19
  • 1970-01-01
  • 2016-06-27
  • 2020-05-19
相关资源
最近更新 更多