[英]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. 我正在使用带有GlassFish的NetBeans构建基于Web服务的简单应用程序。 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.
NetBeans在为新的Web服务及其操作生成代码方面确实提供了很多帮助,但有一件事让我很生气 - Web服务异常处理。 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. 抛出一个巨大的Exceptions细节:java.lang.reflect.InvocationTargetException以及其他大量的异常..我意识到这是一个非常苛刻的问题,但经过几个小时尝试各种各样的事情,我只是不知道该做什么。 I've read about the @WebFault thing but got no idea how to specify this correctly (attach my exception to a particular method..)
我已经阅读了有关@WebFault的内容,但不知道如何正确指定(将我的异常附加到特定方法..)
Please help, all ideas are more than welcome! 请帮忙,欢迎所有想法!
Exceptions that I'm getting: Service invocation threw an exception with message : null; 我得到的例外:服务调用引发了一个异常,其中包含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
Oh ok. 哦好的。 You want to see the "Something is wrong" message.
你想看到“Something is wrong”消息。
So I think the ultimate problem here is that you are not using the standard detailMessage
field in the Throwable
. 所以我认为这里的最终问题是你没有在
Throwable
使用标准的detailMessage
字段。 If you just pass the message to the base class ( super(message);
) then I bet you would see the exception in the trace. 如果你只是将消息传递给基类(
super(message);
)那么我打赌你会在跟踪中看到异常。 Did you try another Exception
type such as just Exception
? 您是否尝试过另一种
Exception
类型,例如Exception
?
You could also define the LoginException.toString()
to be something like: 您还可以将
LoginException.toString()
定义为:
@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);
但我认为我的建议是使用
super(message);
in your constructor. 在你的构造函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.