在 Struts2 应用程序中访问 WebService 异常(SOAP-Fault)
我正在使用 JAX-WS 开发 WebService。在此 WebService 中,我有一个“DatabaseQueryFault”作为 SOAP-Fault:
<wsdl:operation name="executeCustomQuery">
<wsdl:input message="tns:executeCustomQueryRequest"></wsdl:input>
<wsdl:output message="tns:executeCustomQueryResponse"></wsdl:output>
<wsdl:fault name="DatabaseQueryFault" message="tns:DatabaseQueryFault"></wsdl:fault>
</wsdl:operation>
<xsd:element name="databaseQueryFault" type="tns:databaseQueryFault" />
<xsd:complexType name="databaseQueryFault">
<xsd:sequence>
<xsd:element name="reason" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
实现 Java 方法具有此标头
public QueryResponseResultset executeCustomQuery(ExecuteCustomQuery parameters)
throws RemoteException, DatabaseQueryFault {
每次引发异常(例如 SQLException)时,我都会将其“转换”为 DatabaseQueryFault:
} catch (Exception e) {
throw new DatabaseQueryFault(e.getMessage());
}
直到这里为止,它都工作得很好:如果我发送错误的查询,我会收到一个肥皂响应,其中包含 DatabaseQueryFault(包括所有数据)
<soapenv>
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.generalException</faultcode>
<faultstring></faultstring>
<detail>
<ns1:databaseQueryFault xmlns:ns1="http://daga/knowledgebase/webservice">
<reason>Unknown column 'test14' in 'field list'</reason>
</ns1:databaseQueryFault>
<ns2:exceptionName xmlns:ns2="http://xml.apache.org/axis/">
daga.knowledgebase.webservice.DatabaseQueryFault
</ns2:exceptionName>
<ns3:stackTrace xmlns:ns3="http://xml.apache.org/axis/">
但现在问题来了:-) 我的 WebService 客户端是一个 Struts2 Web 应用程序。在那里,我使用了 struts2 (struts.xml) 的本机异常处理:
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
现在我想在 jsp 错误页面中访问肥皂错误的详细信息,但字段为空:
<p>
<s:property value="exception" /><br />
<s:property value="exception.message" /><br />
<s:property value="exception.reason" />
</p>
How can I access the data of my WebService exception in my jsp 文件是什么?
I'm developing a WebService with JAX-WS. In this WebService, I have a "DatabaseQueryFault" as SOAP-Fault:
<wsdl:operation name="executeCustomQuery">
<wsdl:input message="tns:executeCustomQueryRequest"></wsdl:input>
<wsdl:output message="tns:executeCustomQueryResponse"></wsdl:output>
<wsdl:fault name="DatabaseQueryFault" message="tns:DatabaseQueryFault"></wsdl:fault>
</wsdl:operation>
<xsd:element name="databaseQueryFault" type="tns:databaseQueryFault" />
<xsd:complexType name="databaseQueryFault">
<xsd:sequence>
<xsd:element name="reason" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
The implementing Java-method has this header
public QueryResponseResultset executeCustomQuery(ExecuteCustomQuery parameters)
throws RemoteException, DatabaseQueryFault {
Every time an exception is thrown, for example a SQLException, I "transform" it into an DatabaseQueryFault:
} catch (Exception e) {
throw new DatabaseQueryFault(e.getMessage());
}
This works very fine until here: if I send an incorrect query, I get a soap response containing the DatabaseQueryFault including all the data
<soapenv>
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.generalException</faultcode>
<faultstring></faultstring>
<detail>
<ns1:databaseQueryFault xmlns:ns1="http://daga/knowledgebase/webservice">
<reason>Unknown column 'test14' in 'field list'</reason>
</ns1:databaseQueryFault>
<ns2:exceptionName xmlns:ns2="http://xml.apache.org/axis/">
daga.knowledgebase.webservice.DatabaseQueryFault
</ns2:exceptionName>
<ns3:stackTrace xmlns:ns3="http://xml.apache.org/axis/">
But now there comes the problem :-) My WebService Client is a Struts2 web app. There I use the native exception handling of struts2 (struts.xml):
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
Now I want to access the details of my soap error within my jsp error page, but the fields are empty:
<p>
<s:property value="exception" /><br />
<s:property value="exception.message" /><br />
<s:property value="exception.reason" />
</p>
How can I access the data of my WebService exception in my jsp file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您无法直接访问真正的异常,除非您创建并抛出异常。
无论您是否使用 S2 异常处理来处理这种情况(在这种情况下我对此有点“meh”),您都需要将返回消息转换为对用户有价值的东西。您可以将 XML 转换为人类可读的内容,或者创建一个异常来捕获特定于您的应用程序的相关信息。
Well, you don't have direct access to a real exception, unless you create and throw one.
Whether you handle this case with S2 exception handling (I'm kind of "meh" on that in this case) or not, you'll need to turn the return message into something of value to the user. You could transform the XML into something human-readable, or create an exception capturing the relevant information that's specific to your app.