[ 
https://issues.apache.org/jira/browse/CAMEL-10548?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15712058#comment-15712058
 ] 

Luca Burgazzoli commented on CAMEL-10548:
-----------------------------------------

Yes an not :-)

Spring says that it can convert from List to String and it is true but then 
thing is that Spring can't convert from Person to String as 
FallbackObjectToStringConverter can convert POJO if they have a String 
constructor or they have a valueOf(String) method. What spring does under the 
hoods is looping over the collection and search a converter for the each 
element which of course may not be present.

I'm going to submit an issue to Spring but in the meantime I have this hack:

  
https://github.com/lburgazzoli/apache-camel/commit/daf9c98f5d344d62cf551084dd4edb99e6509c52

Would it be acceptable ?

> Converter from List to String is not found when @EnableAutoConfiguration is 
> used
> --------------------------------------------------------------------------------
>
>                 Key: CAMEL-10548
>                 URL: https://issues.apache.org/jira/browse/CAMEL-10548
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-spring-boot
>            Reporter: Luca Burgazzoli
>            Assignee: Luca Burgazzoli
>             Fix For: 2.19.0
>
>
> This very simple spring-boot application :
> {code:java}
> @SpringBootApplication
> public class Application {
>     public static void main(String[] args) {
>         SpringApplication.run(Application.class, args);
>     }
>     @Component
>     public class MyRouteBuilder extends RouteBuilder {
>         @Override
>         public void configure() throws Exception {
>             from("timer:person")
>                 .setBody().constant(Arrays.asList(
>                     new Person("Peter", 25),
>                     new Person("John", 33)
>                 ))
>                 .log("Body is ${body}");
>         }
>     }
>     public static class Person {
>         private String name;
>         private int age;
>         public Person(String name, int age) {
>             this.name = name;
>             this.age = age;
>         }
>         public String getName() {
>             return name;
>         }
>         public void setName(String name) {
>             this.name = name;
>         }
>         public int getAge() {
>             return age;
>         }
>         public void setAge(int age) {
>             this.age = age;
>         }
>         @Override
>         public String toString() {
>             return "Person{" +
>                 "name='" + name + '\'' +
>                 ", age=" + age +
>                 '}';
>         }
>     }
> }
> {code}
> Fails to resolve the simple expression ${body} because of the following 
> exception:
> {code}
> org.apache.camel.TypeConversionException: Error during type conversion from 
> type: java.lang.String to the required type: java.lang.String with value 
> [Person{name='Peter', age=25}, Person{name='John', age=33}] due Failed to 
> convert from type [java.util.Arrays$ArrayList<?>] to type [java.lang.String] 
> for value '[Person{name='Peter', age=25}, Person{name='John', age=33}]'; 
> nested exception is 
> org.springframework.core.convert.ConverterNotFoundException: No converter 
> found capable of converting from type [java.util.Arrays$ArrayList<?>] to type 
> [java.lang.String]
>       at 
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.createTypeConversionException(BaseTypeConverterRegistry.java:629)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:150)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:41)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.builder.ExpressionBuilder$75.evaluate(ExpressionBuilder.java:1795)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.processor.LogProcessor.process(LogProcessor.java:53) 
> ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
>  [camel-core-2.18.0.jar:2.18.0]
>       at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) 
> ~[camel-core-2.18.0.jar:2.18.0]
>       at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 
> ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
>  [camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192)
>  [camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76) 
> [camel-core-2.18.0.jar:2.18.0]
>       at java.util.TimerThread.mainLoop(Timer.java:555) [na:1.8.0_112]
>       at java.util.TimerThread.run(Timer.java:505) [na:1.8.0_112]
> Caused by: org.springframework.core.convert.ConversionFailedException: Failed 
> to convert from type [java.util.Arrays$ArrayList<?>] to type 
> [java.lang.String] for value '[Person{name='Peter', age=25}, 
> Person{name='John', age=33}]'; nested exception is 
> org.springframework.core.convert.ConverterNotFoundException: No converter 
> found capable of converting from type [java.util.Arrays$ArrayList<?>] to type 
> [java.lang.String]
>       at 
> org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:42)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.apache.camel.spring.boot.SpringTypeConverter.convertTo(SpringTypeConverter.java:46)
>  ~[camel-spring-boot-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:346)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       at 
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:133)
>  ~[camel-core-2.18.0.jar:2.18.0]
>       ... 14 common frames omitted
> Caused by: org.springframework.core.convert.ConverterNotFoundException: No 
> converter found capable of converting from type 
> [java.util.Arrays$ArrayList<?>] to type [java.lang.String]
>       at 
> org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.springframework.core.convert.support.CollectionToStringConverter.convert(CollectionToStringConverter.java:68)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       at 
> org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:36)
>  ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
>       ... 19 common frames omitted
> {code}
> The root cause is spring failing to find a converter for Person --> String 
> and as the SpringTypeConverter registered by camel-spring-boot is one of the 
> fallback converters used by BaseTypeConverterRegistry, it leads Camel to fail 
> too.
> As today is a Camel TypeConverter fails with an exception while looping over 
> fallback converters, the loops ends and the conversion fails too. It may turn 
> out that the bug is caused by the way camel uses Spring's type converters but 
> it may be good to catch exceptions and try the next converter, [~davsclaus] 
> make sense ?
> A workaround is to create a custom converter for the class:
> {code:java}
>     @Component
>     public class PersonConverter implements Converter<Person, String> {
>         @Override
>         public String convert(Person source) {
>             return source.toString();
>         }
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to