当前位置: 首页 > 知识库问答 >
问题:

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

徐昕
2023-03-14

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

我的代码:

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

另一方面,我有:

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

当我执行我的代码时,我看到下面的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

如何解决这个问题?

共有2个答案

龙嘉玉
2023-03-14

您必须在META-INF目录中添加名为TypeConver的服务发现文件。

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

该文件包含@Converter类的完全限定名。

例如

mypackage.MultiPartFileToFileConvertor 

另请参阅GitHubhttps://github.com/camelinaction/camelinaction2/tree/b6a43abf9e0d4ec4e3753ebd735bb3448f98194b/chapter3/converter中动作2中骆驼的示例。

此外,本书第3.6章详细解释了自定义转换器的发现和加载是如何工作的。

张俊茂
2023-03-14

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

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

}
 类似资料:
  • 我是阿帕奇骆驼的新手。我正在使用带有Apache Camel和Spring Boot集成的REST API。我已完成获取请求,它按预期工作。但是,在使用 POST 调用发布数据时,我遇到了一个问题。 我在下面的链接中看到了类似的错误。但是,这对我没有帮助。NoTypeConversionAvailableException:没有可从类型转换的类型转换器: 我已经尝试了Stackoverflow,y

  • 我用camel定义了一个处理器,它允许我使用计时器生成一个jaxb-javabean,并将pojo写入xml文件。但当我启动应用程序时,我遇到了以下错误: 08:09:00 WARN[or.ap.ca.co.ti.时间消费者](骆驼(骆驼-1)线程#2-定时器://生成发票)错误处理交换。交换[20E715FDB7EFE19-0000000000000000]。由:[java.io.IOEx 我的

  • Rust 在基本类型之间没有提供隐式类型转换(强制类型转换)(implicit type conversion,coercion )。不过使用 as 关键字进行显式类型转换(explict type conversion,casting)。 一般来说,Rust 的整型类型的转换规则遵循 C 语言的惯例,除了那些在 C 语言是未定义行为的情况。在 Rust 中,所有的整型类型转换的行为都得到了很好的

  • 类型转换可以判断实例的类型,也可以将该实例在其所在的类层次中视为其父类或子类的实例。 Swift 中类型转换的实现为 is 和 as 操作符。这两个操作符使用了一种简单传神的方式来检查一个值的类型或将某个值转换为另一种类型。 如同协议实现的检查(此处应有链接)中描述的那样,你还可以使用类型转换来检查类型是否遵循某个协议。 为类型转换定义类层次 你可以在类及其子类层次中使用类型转换来判断特定类实例的

  • String可以与整型和浮点型互相转换。 整形 String转Int String s("1234"); long value = s.toInt(); Int转String String s(1234); 浮点型 String转Float String s("1234.56"); double value = s.toFloat(); Float转String String s(1234.

  • Variant对象提供的toXXX系列函数可以将标量(Scalar)变量转换为其他类型。Zend字符串、数组、对象可直接使用构造方法进行转换,但必须保证传入的Variant对象必须为该类型,否则底层会抛出致命错误。 转为整型 long value = var.toInt(); 转为浮点型 double value = var.toFloat(); 转为布尔型 bool value = var.