SpringTypeConverter extends TypeConverterSupport { @Override public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException { if (type.getCanonicalName().startsWith("org.apache")) { return null; } if (type.isAssignableFrom(Map.class) && isArrayOrCollection(value)) { return null; } TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf); TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf); for (ConversionService conversionService : conversionServices) { if (conversionService.canConvert(sourceType, targetType)) { try { return (T)conversionService.convert(value, sourceType, targetType); } catch (ConversionFailedException e) { if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) { return null; } else { throw new TypeConversionException(value, type, e); } } } } return null; } @Autowired  SpringTypeConverter(List<ConversionService> conversionServices); @Override T convertTo(Class<T> type, Exchange exchange, Object value);  }
@Test public void testConversionService() { Collection<?> source = Arrays.asList(new Person("Name", 30)); Assert.assertFalse(conversionService.canConvert(Person.class, String.class)); Assert.assertTrue(conversionService.canConvert(source.getClass(), String.class)); try { conversionService.convert(source, String.class); } catch (ConversionFailedException e) { Assert.assertTrue(e.getCause() instanceof ConverterNotFoundException); } Assert.assertNull(converter.convertTo(String.class, source)); }