How to resolve "unsupported Message payload type"

2.4k views Asked by At

I am a stuck... The application reads *.json files from some directory, converts them to MyObject class objects. If class has a type as "PEN" it will save as object.

ObjectType is enum:

public enum ObjType {
    FOOD, PEN, DRINK
}

I have next 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();
}

The application does not save objects. How can I manage a problem connected to 'unsupported Message payload type [l.h.site.MyObject]'. I received next error:

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$000(UnicastingDispatcher.java:53)
at org.springframework.integration.dispatcher.UnicastingDispatcher$3.run(UnicastingDispatcher.java:129)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.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
1

There are 1 answers

0
Artem Bilan On BEST ANSWER

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

As a result of the code:

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() + "]");
            }

So, if you are going to write your MyObject to the file, you should consider to convert it appropriately. And that is exactly the paragraph in the Reference Manual.