2016-12-27 7 views
0

状況:listingTypeの値が列挙型の値を表すものではありませんいくつかの文字列である場合キャメル::ジェネリックリスト<Enum>コンバータ

@Handler 
public void handle(@Header("type") List<ListingType> listingType) { 
    System.err.println(listingType); 
} 

... 

public enum ListingType { 
    TYPE_A, 
    TYPE_B 
} 

、キャメル> <文字列を一覧表示するためにそれを解決します。例えば

は、listingTypeは= "foo" という

System.err.println(listingType); 
=> ["foo"] 

場合、私はFallBackConverter

@FallbackConverter 
public static <T> Object convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) { 
    Class<?> concreteClass = ... 
    if (List.class.isAssignableFrom(type) && concreteClass.isEnum()) { 
     ... 
    } 
} 

カスタムでそれをしようと試みた。しかし、どのように私は、リストの具体的なジェネリック型を得ることができますか?

編集:

または列挙型の具体的なリストに文字列を変換するために、別の解決策はありますか?

答えて

1

実行時には実行できません。 Javaのジェネリックスはコンパイル段階でのみ存在し、とにかくプログラムには影響しません。

Hereジェネリックスについて詳しく読むことができます。

0

私は、正しい列挙値の代わりに文字列のリストを最初に保持している理由はわかりません。
私はそれを処理する前に明示的にヘッダーを変換しようとします。

<route> 
    <from uri="direct:yourRoute"> 
    <to uri="bean:preprocessHeader" /> <!-- this converts the header to enum --> 
    <to uri="bean:handle" /> <!-- this is your bean/method --> 
</route> 

テストされていません

public process(Exchange exchange) { 
    List<String> stringHeader = (List<String>)exchange.getIn().getHeader("type", List.class); 

    List<ListingType> enumHeader = new ArrayList<>(stringHeader.size()); 

    for (String h : stringHeader) { 
     try { 
      ListingType t = ListingType.valueOf(h); 
      enumHeader.add(t); 
     } carch (IllegalArgumentException e) { 
      // Handle the case where the string is not a proper enum value 
      enumHeader.add(ListingType.UNKNOWN); 
     } 
    } 

    exchange.getIn().setHeader("type", enumHeader); 
} 

コードのような方法でpreprocessHeader Beanを定義します。

+0

私は毎回それを実装したくありません。そのCamelはTypeConvertersを提供しています。 –

+0

@BenKeil StringからListTypeへの通常のTypeConverterは動作しますか?もしそうなら、私は文字列をhederに追加する前に文字列を変換しようとします。 –

+0

いいえ動作しません。 –