view raw
Mike Mike - 6 months ago 51
Java Question

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

auth_code = val.return_AuthCode();
return auth_code;

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


and the exception class:

public class LoginException extends Exception
String message;

public LoginException(String message)
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(
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(
at org.glassfish.webservices.EjbWebServiceServlet.service(
at javax.servlet.http.HttpServlet.service(
at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.doFilter(
at com.sun.grizzly.http.servlet.ServletAdapter$FilterChainImpl.invokeFilterChain(
at com.sun.grizzly.http.servlet.ServletAdapter.doService(
Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(
... 24 more
Caused by: bank_services.LoginException_Exception: excepts.LoginException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
at java.lang.reflect.Constructor.newInstance(
at $Proxy383.checkUserExist(Unknown Source) ... 29 more


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:

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.