我正在尝试创建一个简单的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)
P.S。我是骡子的新手。所以我也对任何其他优雅方法持开放态度。
答案 0 :(得分:0)
对于Jersey来说,实现回调没有多大意义,你应该使用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。
答案 1 :(得分:0)
在java组件中实现Callable为它提供了一个从流xml中调用的功能。在您的情况下,Callable不需要使用JAX-RS注释来注释您的服务类,以便jersey可以在您的类中发布公共方法。当您发布地址类似于服务类路径的请求时,jersey将自动调用相应的方法。
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("restClass")
public class RestClass {
public Response getExample(@QueryParam("param1") String param1) {
return Response.status(Status.OK).entity("hello " + param1).build();
}
}
这应该适合你。