我上的應用程序,並在任何情況下發生異常它打印異常信息工作appropriately.Here是我的自定義過濾器異常
package filter;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.mule.api.MuleMessage;
public class ExceptionClass implements org.mule.api.routing.filter.Filter {
@Override
public boolean accept(MuleMessage message) {
Map<String,Object> payload=(Map<String,Object>)message.getPayload();
if(!payload.containsKey("productid"))
{
throw new java.lang.NullPointerException("no data found in payload "+payload.toString());
}
if(Integer.parseInt((String) payload.get("productid"))<5)
{
throw new IllegalArgumentException("invalid input to payload " +payload.toString());
}
return true;
}
}
下面是應用程序的配置
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="muleexceptionhandlingFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP" >
<http:response-builder statusCode="#[flowVars['statuscode']]" reasonPhrase="#[flowVars['reason']]"/>
</http:listener>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
<custom-filter doc:name="Custom" class="filter.ExceptionClass"/>
<logger message="payload:#[payload]" level="INFO" doc:name="Logger"/>
<logger message="#[message]" level="INFO" doc:name="Logger"/>
<set-payload value="payload:#[payload]" doc:name="Set Payload"/>
</flow>
<choice-exception-strategy name="muleexceptionhandlingChoice_Exception_Strategy">
<catch-exception-strategy doc:name="Catch Missing Data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.NullPointerException)]">
<set-payload value="Missing data:#[payload]" doc:name="Set Payload"/>
<set-variable variableName="reason" value="missing input data" doc:name="Variable"/>
<set-variable variableName="statuscode" value="400" doc:name="Variable"/>
</catch-exception-strategy>
<catch-exception-strategy doc:name="Catch Invalid data Exception Strategy" logException="false" when="#[exception.causedBy(java.lang.IllegalArgumentException)}">
<set-payload value="Invalid Data:#[payload]" doc:name="Set Payload"/>
<set-variable variableName="reason" value="invalid input" doc:name="Variable"/>
<set-variable variableName="statuscode" value="400" doc:name="Variable"/>
</catch-exception-strategy>
</choice-exception-strategy>
</mule>
,這裏是在空指針異常的情況下,該錯誤消息
ERROR 2017年7月25日17:35:49595 [[muleexceptionhandling] .HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: **************************** ************************************************** **消息:在有效負載中找不到數據{price = 1000, productname = Shampoo}(java.lang.NullPointerException)。有效載荷
:{價格= 1000,產品名=洗髮}有效載荷類型: java.util.HashMap中篩選: [email protected]元素: /muleexceptionhandlingFlow /處理器/ 1 @ muleexceptionhandling:空:空 --- -------------------------------------------------- ---------------------------根異常堆棧跟蹤:java.lang.NullPointerException:在有效負載中找不到數據 {price = 1000,productname =洗髮}在 filter.ExceptionClass.accept(ExceptionClass.java:23)處 org.mule.processor.AbstractFilteringMessageProcessor.process org.mule.routing.MessageFilter.accept(MessageFilter.java:93)(AbstractFilteringMe 。 ..... ............................
爲什麼例外沒有得到例外處理程序捕獲
請指導!
非常感謝您的回答。其實我昨天就開始研究它,所以不太瞭解它:) –
歡迎您:) – jvas