在Spring BlazeDS Integration 集成环境中,我们尝试抛出以下自定义异常
public void remove() throws Exception { try { throw new LmxException("删除失败!"); } catch (LmxException e) { throw e; } }
在Flex端是这样捕获的
private function operateFaultHandler(event:FaultEvent):void{ var exception:LmxException = event.fault.rootCause as LmxException; Alert.show(exception.message,"提示"); }
但是抛出了There was an unhandled failure on the server.Spring BlazeDS Integration 的原文DOC如下。小弟英语不好,
简单翻译几句,服务器端抛出异常时,为了让Flex端获取到更为友好的信息,原始的异常的信息必须能够被解析成MessageException类的实例
2.8. Using Custom Exception Translators
In order to propagate useful information back to the Flex client when an exception occurs on the server, the original exception must be translated into an instance of flex.messaging.MessageException. If special translation logic is not applied, a generic "Server.Processing" error will propagate to the client that doesn't give the client the chance to reason on the real cause of the error to take appropriate action. Special exception translators are configured by default for transforming Spring Security exceptions into an appropriate MessageException, but it could also be useful to provide custom translation for your own application-level exceptions.
Custom exception translation logic can be provided through implementations of the org.springframework.flex.core.ExceptionTranslator interface. These implementations must be configured as Spring beans and then registered through the XML configuration namespace as follows:
自定义异常解析逻辑可以由实现ExceptionTranslator 的接口实现,这些实现类必须按照以下的配置,LmxException 是自定义异常类
public class ExceptionTranslatorImpl implements ExceptionTranslator { @Override public boolean handles(Class> clazz) { // TODO Auto-generated method stub return Boolean.TRUE; } @Override public MessageException translate(Throwable throwable) { // TODO Auto-generated method stub MessageException messageException = null; if (throwable != null) { if (throwable instanceof LmxException) { messageException = new MessageException(); LmxException lmxException = new LmxException(); lmxException.setMessage("异常"); messageException.setRootCause(lmxException); } } return messageException; } }
<!-- Custom exception translator configured as a Spring bean --> <bean id="myExceptionTranslator" class="com.foo.app.MyBusinessExceptionTranslator"/> <flex:message-broker> <flex:exception-translator ref="myExceptionTranslator"/> </flex:message-broker>