org.apache.camel.NoTypeConversionAvailableException:没有可用于从类型转换的类型转换器:

我首先有一个多部分文件,我想将它发送到骆驼管道并用原始名称保存这个文件.

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

我的代码:

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

另一方面,我有:

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

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

我也尝试注册转换器:

@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

如何解决这个问题?

推荐答案

使用 SpringBoot 的类型转换器的示例,将使用 Camel 2.23.0 或更高版本自动拾取.

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);
    }
  }

}

相关文章