簡體   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