복붙노트

[SPRING] org.apache.camel.NoTypeConversionAvailableException : 형식 변환 할 수있는 형식 변환기가 없습니다 :

SPRING

org.apache.camel.NoTypeConversionAvailableException : 형식 변환 할 수있는 형식 변환기가 없습니다 :

나는 처음부터 multipart 파일을 가지고 있으며 그것을 낙타 파이프 라인으로 보내 원래 이름으로이 파일을 저장하려고합니다.

내 코드 :

@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

이 문제를 해결하는 방법?

해결법

  1. ==============================

    1.TypeConverter라는 이름의 서비스 검색 파일을 META-INF 디렉토리에 추가해야합니다.

    TypeConverter라는 이름의 서비스 검색 파일을 META-INF 디렉토리에 추가해야합니다.

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

    이 파일에는 정규화 된 @Converter 클래스.

    전자

    mypackage.MultiPartFileToFileConvertor 
    

    또한 Github https://github.com/camelinaction/camelinaction2/tree/b6a43abf9e0d4ec4e3753ebd735bb3448f98194b/chapter3/converter의 Action 2에있는 Camel의 예를 참조하십시오.

    또한 책 3.6 장에서는 사용자 정의 변환기의 발견 및로드가 어떻게 자세하게 작동하는지 설명합니다.

  2. ==============================

    2.스프링 부트를 사용하는 형식 변환기의 예 : Camel 2.23.0 이상을 사용하여 자동으로 선택됩니다.

    스프링 부트를 사용하는 형식 변환기의 예 : 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);
        }
      }
    
    }
    
  3. from https://stackoverflow.com/questions/46952090/org-apache-camel-notypeconversionavailableexception-no-type-converter-available by cc-by-sa and MIT license