org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type:

21,577

Solution 1

An example of a type converter using SpringBoot which will be automatically picked up using Camel 2.23.0 or later.

@Component
public class MyPackageTypeConverter implements TypeConverters {

  private final ObjectMapper mapper;

  @Autowired
  public MyPackageTypeConverter(ObjectMapper mapper) {
    this.mapper = mapper;
  }

  @Converter
  public byte[] myPackageToByteArray(MyPackage source) {
    try {
      return mapper.writeValueAsBytes(source);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  @Converter
  public MyPackage byteArrayToMyPackage(byte[] source) {
    try {
      return mapper.readValue(source, MyPackage.class);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

}

Solution 2

You have to add the service discovery file named TypeConverter in the META-INF directory.

src\main\resources\META-INF\services\org\apache\camel\TypeConverter 

This file contains the fully qualified name of the @Converter class.

e.g

mypackage.MultiPartFileToFileConvertor 

Also see the example from Camel in action 2 in Github https://github.com/camelinaction/camelinaction2/tree/b6a43abf9e0d4ec4e3753ebd735bb3448f98194b/chapter3/converter .

Also the book Chapter 3.6 explains how the discovery and loading of custom converters work in full detail.

Share:
21,577

Related videos on Youtube

gstackoverflow
Author by

gstackoverflow

Updated on January 23, 2020

Comments

  • gstackoverflow
    gstackoverflow about 4 years

    I have multipart file in the first place and I want to send it to camel pipeline and save this file with original name.

    my code:

    @Autowired
    ProducerTemplate producerTemplate;
    ...
          producerTemplate.sendBody("seda:rest_upload", multipartFile);
    

    on another side I have:

    from("seda:rest_upload").convertBodyTo(File.class).to("file://rest_files");

    And also I try to register converter:

    @Converter
    public class MultiPartFileToFileConvertor {
        @Converter
        public static File toFile(MultipartFile multipartFile) throws IOException {
            File convFile = new File(multipartFile.getOriginalFilename());
            multipartFile.transferTo(convFile);
            return convFile;
        }
    }
    

    When I execute my code I see following stacktrace:

    Message History
    ---------------------------------------------------------------------------------------------------------------------------------------
    RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
    [route2            ] [route2            ] [seda://rest_upload                                                            ] [         3]
    [route2            ] [convertBodyTo1    ] [convertBodyTo[java.io.File]                                                   ] [         2]
    
    Stacktrace
    ---------------------------------------------------------------------------------------------------------------------------------------
    
    org.apache.camel.InvalidPayloadException: No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]
        at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:113) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.ConvertBodyProcessor.process(ConvertBodyProcessor.java:91) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:298) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:210) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:155) [camel-core-2.20.0.jar:2.20.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
    Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7
        at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:111) ~[camel-core-2.20.0.jar:2.20.0]
        ... 12 common frames omitted
    
    2017-10-26 13:30:58.447  WARN 19608 --- [a://rest_upload] o.a.camel.component.seda.SedaConsumer    : Error processing exchange. Exchange[]. Caused by: [org.apache.camel.InvalidPayloadException - No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]]
    
    org.apache.camel.InvalidPayloadException: No body available of type: java.io.File but has value: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7 of type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile on: Message[ID-ntkachev-1509013331141-0-13]. Caused by: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7. Exchange[ID-ntkachev-1509013331141-0-14]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7]
        at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:113) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.ConvertBodyProcessor.process(ConvertBodyProcessor.java:91) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:298) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:210) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:155) [camel-core-2.20.0.jar:2.20.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
    Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile to the required type: java.io.File with value org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@24c9ecc7
        at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:111) ~[camel-core-2.20.0.jar:2.20.0]
        ... 12 common frames omitted
    

    How to fix this ?

    • Souciance Eqdam Rashti
      Souciance Eqdam Rashti over 6 years
      And if you don't convert to File type? Just send it as it is to be saved?
    • gstackoverflow
      gstackoverflow over 6 years
      Are suggest to create file before sendBody invocation and send it?
    • Souciance Eqdam Rashti
      Souciance Eqdam Rashti over 6 years
      I mean change from: from("seda:rest_upload").convertBodyTo(File.class).to("file:‌​//rest_files"); to from("seda:rest_upload").to("file://rest_files");
    • gstackoverflow
      gstackoverflow over 6 years
      org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.springframework.web.multipart.support.StandardMultipartH‌​ttpServletRequest.St‌​andardMultipartFile to the required type: java.io.InputStream with value org.springframework.web.multipart.support.StandardMultipartH‌​ttpServletRequest$St‌​andardMultipartFile@‌​9c5312b
    • Souciance Eqdam Rashti
      Souciance Eqdam Rashti over 6 years
      Yeah, but can't you convert the multipart to a byte format and put the byte on the exchange? Then you should not get the typeconversion error
  • gstackoverflow
    gstackoverflow over 6 years
    is it service provide interface?
  • ltsallas
    ltsallas over 6 years
    I didn't understand your comment. Did you add the file in your resources?
  • ltsallas
    ltsallas over 6 years
    Came scans and loads converters automatically with this github.com/apache/camel/blob/master/camel-core/src/main/java‌​/…
  • ltsallas
    ltsallas over 6 years
    @gstackoverflow Got any feedback on this?
  • Gerry
    Gerry over 2 years
    Source moved to github.com/Talend/apache-camel/blob/master/camel-core/src/ma‌​in/… although seems like reading the docs should be enough