camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cristiano Costantini <>
Subject Re: TypeConverting to null and
Date Thu, 30 May 2013 13:01:55 GMT
thank you for your kind response. :-)
I'm satisfied now as I have a good vision and I can make the right choices
in my context.

Just for the sake of completeness,
my usage of the type converter was "inside" void process(Exchange)  method,
and I was invoking the type converter explicitly this way:

Object result = aRefelctedMethod.invoke(obj, args);
TypeConverter typeConverter = exchange.getContext().getTypeConverter();
MyPayload resultPayload = typeConverter.convertTo(MyPayload.class,
exchange, result);

I used the Camel Type Converter because I'm not aware of the result Object
as it comes from a class not aware to this context.

The camel type converter is useful as it resolve which type of conversion
to use depending on the type of result.

In the end, the quickest way to fix it, is to have my "good" type converter
return an instance of "MyEmptyPayload". I could do a if(result instanceof
MyEmptyPayload) { exchange.getOut().setBody(null); } but in my case it was
best to change consumers of the exchange tolerate the MyEmptyPayload

Thank you again,
as I'm quite new to type converter API, feedback on the style is welcome!


2013/5/30 Christian Müller <>

> You cannot use the type converter in this case. Camel is throwing a
> NoTypeConversionAvailableException if the type converter returns null and
> no other suitable type converter could be found. Please have a look at my
> attached sample project...
> I suggest to use a bean/processor afterwards to set the body to null (e.g.
> if this special header entry exists).
> Best,
> Christian Müller
> -----------------
> Software Integration Specialist
> Apache Camel committer:
> V.P. Apache Camel:
> Apache Member:
> On Wed, May 29, 2013 at 12:48 PM, Cristiano Costantini <
>> wrote:
>> Hi All,
>> I have a custom type converter to a custom data model.
>> For my purposes, some input types need to be converted to null and  the
>> exchange need to be processed to add an header even if conversion results
>> in null.
>> For example (simplified):
>> @Converter
>> public static MyPayload convert(MessageContentsList message, Exchange
>> exchange) {
>>   if (exchange != null) {
>>     exchange.getIn().getHeaders().put("newHeaderName",
>> "someNewHeaderValue");
>>   }
>>   return null;
>> }
>> Well, the first time the converter is invoked it works (the exchange has
>> the new header),
>> The second time invokation of "convert" get skipped in
>> BaseTypeConverterRegistry.doConvertTo(...) as the following if statement
>> resolves to true:
>>         // check if we have tried it before and if its a miss
>>         TypeMapping key = new TypeMapping(type, value.getClass());
>>         if (misses.containsKey(key)) {
>>             // we have tried before but we cannot convert this one
>>             return Void.TYPE;
>>         }
>> I've seen that there have been "semantical ambiguity of null"  in the
>> past,
>> which should have been solved in issue CAMEL-84, but I've not understood
>> it
>> and how I should handle this scenario in a proper way.
>> I guess I'm using type converter in a bad style, but as I don't understand
>> why I can't guess the good way.
>> The only idea I have is to create a class MyEmptyPayload implements
>> MyPayload { } and return an instance to this instead of null, but I still
>> would like to understand the reasons.
>> Could someone orient me toward the right solution?
>> Thank you very much,
>> Cristiano

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message