繁体   English   中英

自定义异常处理 - Java Web服务

[英]Custom exceptions handling - java Web Services

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

我正在使用带有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;
    }
}

抛出一个巨大的Exceptions细节:java.lang.reflect.InvocationTargetException以及其他大量的异常..我意识到这是一个非常苛刻的问题,但经过几个小时尝试各种各样的事情,我只是不知道该做什么。 我已经阅读了有关@WebFault的内容,但不知道如何正确指定(将我的异常附加到特定方法..)

请帮忙,欢迎所有想法!

我得到的例外:服务调用引发了一个异常,其中包含message: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             

哦好的。 你想看到“Something is wrong”消息。

所以我认为这里的最终问题是你没有在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); 在你的构造函数中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM