Adr*_*lat 6 java spring web-services jax-ws jax-ws-customization
默认情况下,当RuntimeException在我的服务器上发生扩展的未捕获异常时,JAX-WS会构建以下SOAP错误消息:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>[runtime exception message here]</faultstring>
<detail>
<ns2:exception class="java.lang.RuntimeException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false" xmlns:ns2="http://jax-ws.dev.java.net/">
<message>[runtime exception message here too]</message>
<ns2:stackTrace>
[stack trace details]
</ns2:stackTrace>
</ns2:exception>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
Run Code Online (Sandbox Code Playgroud)
哪种有意义,除了我想改变那种行为以便发送它:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>Something wrong happened and it's totally our fault</faultstring>
</S:Fault>
</S:Body>
</S:Envelope>
Run Code Online (Sandbox Code Playgroud)
请注意,消息不应该是RuntimeException的消息内容,而是针对可能在服务器端进行扩展的任何异常的自定义静态消息.RuntimeException
我无法更改WSDL,我不想设置自定义异常.
我正在使用spring插件: com.sun.xml.ws.transport.http.servlet.WSSpringServlet
我怎样才能做到这一点?
我认为您可以使用 SoapFaultMappingExceptionResolver http://docs.spring.io/spring-ws/site/reference/html/server.html解决问题
SoapFaultMappingExceptionResolver 是一个更复杂的实现。该解析器使您能够获取可能引发的任何异常的类名并将其映射到 SOAP 错误,如下所示:
<beans>
<bean id="exceptionResolver"
class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
<property name="defaultFault" value="SERVER"/>
<property name="exceptionMappings">
<value>
org.springframework.oxm.ValidationFailureException=CLIENT,Invalid request
</value>
</property>
</bean> </beans>
Run Code Online (Sandbox Code Playgroud)
键值和默认端点使用格式faultCode,faultString,locale,其中仅需要故障代码。如果未设置故障字符串,则默认为异常消息。如果未设置语言,则默认为英语。上述配置将把 ValidationFailureException 类型的异常映射到带有错误字符串“Invalid request”的客户端 SOAP 错误,如以下响应所示:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>Invalid request</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
如果发生任何其他异常,它将返回默认错误:服务器端错误,异常消息作为错误字符串。
您应该将 org.springframework.oxm.ValidationFailureException 异常更改为您感兴趣的异常,即 java.lang.Exception 或 java.lang.RuntimeException
您还可以创建自定义异常类
public class CustomGenericAllException extends RuntimeException {
private String errorCode;
private String errorMsg;
//getter and setter for errorCode and errorMsg
public CustomGenericAllException(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}
Run Code Online (Sandbox Code Playgroud)
在每个方法中你都可以抛出这个异常
throw new CustomGenericAllException("S:Server", "Something wrong happened and it's totally our fault");
Run Code Online (Sandbox Code Playgroud)
在 xml 配置中,您可以映射此通用异常
<value>com.testpackage.CustomGenericAllException ...。
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
2445 次 |
| 最近记录: |