我很困难......应用程序从某个目录中读取* .json文件,将它们转换为MyObject类对象。如果类的类型为" PEN"它将保存为对象。
ObjectType是枚举:
public enum ObjType {
FOOD, PEN, DRINK
}
我有下一堂课:
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();
}
应用程序不保存对象。 如何管理连接到不支持的消息有效内容类型[l.h.site.MyObject]&#39;的问题。 我收到了下一个错误:
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
答案 0 :(得分:0)
引起:java.lang.IllegalArgumentException:不支持的消息有效内容类型[l.h.site.MyObject] 在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中的段落。