我正在尝试创建一个简单的 mule 流,它提取标头并将其传递user-agent
给 REST 组件,该组件根据提取的用户代理返回状态代码。
这是我的骡流
<flow name="restflowFlow1" doc:name="restflowFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9595" path="rest" doc:name="HTTP"/>
<jersey:resources doc:name="REST">
<component class="com.test.RestClass"/>
</jersey:resources>
</flow>
这是对应的类
@Path("restClass")
public class RestClass implements Callable {
public Response getExample(String toBeValidated)
{
if(toBeValidated.contains("Apple"))
{
return Response.status(Status.OK).entity("hello " + toBeValidated).build();
}
return Response.status(Status.UNAUTHORIZED).entity("hello " + toBeValidated).build();
}
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
String requiredHeader= eventContext.getMessage().getProperty("user-agent", PropertyScope.INBOUND);
return getExample(requiredHeader);
}
}
当我尝试运行上述流程时,出现以下错误:
ERROR 2014-11-21 13:49:47,909 [[muletestproject].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Failed to invoke JerseyResourcesComponent{restflowFlow1.component.418586223}. Component that caused exception is: JerseyResourcesComponent{restflowFlow1.component.418586223}. Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. String index out of range: -1 (java.lang.StringIndexOutOfBoundsException)
java.lang.String:1875 (null)
2. Failed to invoke JerseyResourcesComponent{restflowFlow1.component.418586223}. Component that caused exception is: JerseyResourcesComponent{restflowFlow1.component.418586223}. Message payload is of type: String (org.mule.component.ComponentException)
org.mule.component.AbstractComponent:144 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1875)
PS我对骡子很陌生。所以我也对任何其他优雅的方法持开放态度。