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

时间秒杀一切 提交于 2019-12-02 04:05:53

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.

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!