gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:15:11 24 4
gpt4 key购买 nike

第一次在这里发帖,请大家耐心指正...

我正在使用 NetBeans 和 GlassFish 构建简单的基于 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;
}
}

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

请帮忙,欢迎所有想法!

我得到的异常:服务调用抛出异常消息: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

最佳答案

哦好的。您想看到“出了点问题”消息。

所以我认为这里的最终问题是您没有在 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);

关于java - 自定义异常处理 - java Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com