Situation:
@Handler
public void handle(@Header("type") List<ListingType> listingType) {
System.err.println(listingType);
}
...
public enum ListingType {
TYPE_A,
TYPE_B
}
If the value of listingType is some String that doesn't represent a value of the Enum, Camel resolves it to List<String>.
For example, if listingType = "foo"
System.err.println(listingType);
=> ["foo"]
I tryed it with a custom 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()) {
...
}
}
But how can I get the concrete generic type of the list?
Edited:
Or is there another solution to convert the String to the concrete list of enums?
You can't do it at Runtime. Generics in Java present only at compilation stage and don't affect on the program anyway.
Here you can read more about the Generics.
I don't know why you are keeping a list of strings in the first place instead of proper enum values.
I would try to explicitly convert the header before processing it.
<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>
Define a preprocessHeader
bean with a method like
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);
}
Code not tested.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.