我正在尝试创建一个简单的 Mule 流,用于提取 header 并将 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)
附注我对骡子很陌生。所以我也对任何其他优雅的方法持开放态度。
最佳答案
要实现可调用对 Jerse 来说很有意义,您应该使用 JAX-RS 注释,即:
package org.mule.transport.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
@Path("/helloworld")
public class HelloWorldResource {
@GET
@Produces("text/plain")
@Path("/{name}")
public String sayHelloWithUri(@PathParam("name") String name) {
return "Hello " + name;
}
}
如上所述here .
关于java - 我对这种骡子 Jersey 流程做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071088/