1

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 Im はラバにはまったく新しいものです。したがって、私は他のエレガントなアプローチにもオープンです。

4

2 に答える 2

0

Java コンポーネントに Callable を実装すると、フロー xml から呼び出される機能が提供されます。あなたの場合、Jerseyがクラスのパブリックメソッドを公開できるように、サービスクラスにJAX-RSアノテーションでアノテーションを付けるだけのCallableは必要ありません。サービス クラスのパスに似たアドレスでリクエストを送信すると、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();
  }
}

これはうまくいくはずです。

于 2015-09-09T13:14:34.543 に答える