Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom exceptions handling - java Web Services

Its my first time posting here so please be patient and correct me whenever necessary...

I am building simple web service-based application using NetBeans with GlassFish. NetBeans does help a lot in terms of generating code for new web services and their operations, one thing drives me mad though - web service exception handling. Operation like:

@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!");

}

and the exception class:

public class LoginException extends Exception
{
    String message;


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

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

throws a massive Exceptions details : java.lang.reflect.InvocationTargetException plus tons of other exceptions.. I realise it is a very much so nooby question, but after many hours of trying various things I just do not know what tot do. I've read about the @WebFault thing but got no idea how to specify this correctly (attach my exception to a particular method..)

Please help, all ideas are more than welcome!

Exceptions that I'm getting: Service invocation threw an exception with message : null; Refer to the server log for more details

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             
like image 533
Mike Avatar asked Oct 10 '22 07:10

Mike


1 Answers

Oh ok. You want to see the "Something is wrong" message.

So I think the ultimate problem here is that you are not using the standard detailMessage field in the Throwable. If you just pass the message to the base class (super(message);) then I bet you would see the exception in the trace. Did you try another Exception type such as just Exception?

You could also define the LoginException.toString() to be something like:

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

Alternatively, you will need to do something like this:

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);
    }
}

But I think my recommendation is to use super(message); in your constructor.

like image 67
Gray Avatar answered Oct 13 '22 10:10

Gray