如何解决"unsupported Message payload type"

How to resolve "unsupported Message payload type"

我被卡住了...应用程序从某个目录读取 *.json 文件,将它们转换为 MyObject class 对象。如果 class 的类型为 "PEN" 它将保存为对象。

对象类型是枚举:

public enum ObjType {
    FOOD, PEN, DRINK
}

我有下一个class:

public static void main(String[] args) throws Exception{
    SpringApplication.run(DemoApplication.class, args);
}

@Bean
public IntegrationFlow fileReading() {
    return IntegrationFlows
            .from(s -> s.file(new File(DIR))
                    .patternFilter(FILE_END),
                  e -> e.poller(Pollers.fixedDelay(500)))
            .transform(new JsonToObjectTransformer(MyObject.class), mth -> mth.async(true))
            .filter(MyObject.class, s -> 
                        s.getType() != null 
                        && s.getType().toString().length() > 0 
                        && Arrays.toString(ObjType.values()).contains(s.getType().toString()))
            .<MyObject, ObjType>route(MyObject::getType, mapping -> mapping
                    .ignoreSendFailures(true)
                    .defaultOutputToParentFlow()
                    .subFlowMapping(ObjType.FOOD, sf -> sf
                        .handle( p -> {
                            printMessage((MyObject)p.getPayload());
                        }))
                    .subFlowMapping(ObjType.PEN, sf -> sf
                        .channel(mc->mc.executor(Executors.newCachedThreadPool()))
                        .enrichHeaders(h->h
                                .header(MessageHeaders.CONTENT_TYPE, MyObject.class)
                                .shouldSkipNulls(true))
                        .handleWithAdapter(
                            a -> a
                                .file(new File(SAVE_DIR))
                                .autoCreateDirectory(true)
                                .deleteSourceFiles(true)
                                .fileNameGenerator(message -> {
                                    MyObject mo = (MyObject)message.getPayload();
                                    String head = message.getHeaders().getId().toString();
                                    return head;
                                })
                                .fileExistsMode(FileExistsMode.REPLACE)
                            , c->c.requiresReply(false)
                                .autoStartup(true)
                                .async(true)
                                .id("writeFile")
                            )
                        )
                    )
            .get();
}

应用程序不保存对象。 如何解决与 'unsupported Message payload type [l.h.site.MyObject]' 相关的问题。 我收到下一个错误:

2017-09-15 16:01:58.118  INFO 10116 --- [ask-scheduler-3] o.s.i.file.FileReadingMessageSource      : Created message: [GenericMessage [payload=c:\JsonInput\something - Copy - Copy.json, headers={id=519f3cfc-8088-fe92-7663-1f3ce6b5f3e7, timestamp=1505480518118}]]
2017-09-15 16:01:58.118 ERROR 10116 --- [ool-13-thread-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageHandlingException: failed to write Message payload to file; nested exception is java.lang.IllegalArgumentException: unsupported Message payload type [l.h.site.MyObject], failedMessage=GenericMessage [payload=l.h.site.MyObject@3d06bfbf, headers={contentType=class l.h.site.MyObject, id=e9d37006-d970-f55a-3961-d5f614dc681f, timestamp=1505480518118}]
at org.springframework.integration.file.FileWritingMessageHandler.handleRequestMessage(FileWritingMessageHandler.java:483)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:425)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:375)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:360)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:271)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:188)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
at org.springframework.integration.dispatcher.UnicastingDispatcher.access[=13=]0(UnicastingDispatcher.java:53)
at org.springframework.integration.dispatcher.UnicastingDispatcher.run(UnicastingDispatcher.java:129)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.run(ErrorHandlingTaskExecutor.java:55)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: unsupported Message payload type [l.h.site.MyObject]
at org.springframework.integration.file.FileWritingMessageHandler.handleRequestMessage(FileWritingMessageHandler.java:468)
... 24 more

Caused by: java.lang.IllegalArgumentException: unsupported Message payload type [l.h.site.MyObject] at org.springframework.integration.file.FileWritingMessageHandler.handleRequestMessage(FileWritingMessageHandler.java:468)

代码结果:

if (payload instanceof File) {
                resultFile = handleFileMessage((File) payload, tempFile, resultFile);
            }
            else if (payload instanceof InputStream) {
                resultFile = handleInputStreamMessage((InputStream) payload, originalFileFromHeader, tempFile,
                        resultFile);
            }
            else if (payload instanceof byte[]) {
                resultFile = this.handleByteArrayMessage(
                        (byte[]) payload, originalFileFromHeader, tempFile, resultFile);
            }
            else if (payload instanceof String) {
                resultFile = this.handleStringMessage(
                        (String) payload, originalFileFromHeader, tempFile, resultFile);
            }
            else {
                throw new IllegalArgumentException(
                        "unsupported Message payload type [" + payload.getClass().getName() + "]");
            }

因此,如果您要将 MyObject 写入文件,您应该考虑适当地转换它。那正是 Reference Manual.

中的段落