我有一个 JAX-WS Web 服务,其 Web 方法可能会引发异常。
@WebMethod
public Folder getTree() throws UnauthorizedException {
//...
// Get authorization data..
//...
if (!authorized) {
throw new UnauthorizedException();
}
//...
}
如果用户获得授权,它可以正常工作,但是当抛出异常时,它不会生成带有错误的 SOAP 消息,它只会导致 Web 服务崩溃
SEVERE: Unauthorized
ru.cos.xdoc.storage.UnauthorizedException: Unauthorized
at ru.cos.xdoc.storage.Storage.getTree(Storage.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
并关闭连接
HTTP/1.1 500 Internal Server Error
X-Powered-By: Servlet/2.5
Server: Sun GlassFish Enterprise Server v2.1.1
Content-Type: text/xml;charset="utf-8"
Transfer-Encoding: chunked
Date: Mon, 20 Sep 2010 15:43:59 GMT
Connection: close
我觉得我错过了一些简单的事情
编辑 事实证明,只有在 Web 方法开始后不久抛出异常的情况下才会出现该问题。当在抛出异常之前引入延迟时,例如
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
throw new UnauthorizedException();
一切正常。
有人知道什么可能导致这种奇怪的行为吗?
最佳答案
您是否已将异常映射到故障?请参阅JAX-WS - Map Exceptions to faults
关于java - 在 JAX-WS 中引发自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3753174/