【问题标题】:What am I doing wrong with this mule -jersey flow?我在这个 mule-jersey 流中做错了什么?
【发布时间】:2014-11-21 21:59:35
【问题描述】:

我正在尝试创建一个简单的 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)

附:我对骡子很陌生。所以我也愿意接受任何其他优雅的方法。

【问题讨论】:

    标签: java jersey mule mule-component


    【解决方案1】:

    要实现对 Jerse 很有意义的 callable,您应该使用 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所述。

    【讨论】:

    • 为什么@Context HttpServletRequest 返回null?
    • 您没有在 Servet 服务器中运行。有一些方法可以在 Mule 中嵌入一个 servet 服务器,但在许多用例中这可能是一种矫枉过正的做法。你能分享一下你需要它的原因吗?
    • 我想公开一个球衣休息服务,我应该能够使用@Context HttpServletRequest 获取请求信息。注意在我的情况下,我使用的是 servlet 连接器而不是 http 连接器,并且我在 Tomcat 服务器中托管服务
    【解决方案2】:

    在 java 组件中实现 Callable 使其具有从流 xml 调用的功能。在您的情况下,不需要 Callable 只需使用 JAX-RS 注释注释您的服务类,以便球衣可以在您的类中发布公共方法。当您使用类似于您的服务类路径的地址发布请求时,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();
      }
    }
    

    这应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多