I'm using SpringXD where I have a Rabbit Source with outputType application/json. Next module receive it and convert it to Java Object. RabbitMq is my transport bus.
My configuration is pretty standard like:
@EnableIntegration
@ImportResource("classpath:some.xml")
@ComponentScan(basePackages = "com.something")
public class MyConfigClass {
@Autowired
private Jackson2JsonObjectMapper jackson2JsonObjectMapper;
@Bean
public MessageChannel input() {
return new DirectChannel();
}
@Bean
public MessageChannel inputObject() {
return new DirectChannel();
}
@Bean
public MessageChannel output() {
return new DirectChannel();
}
@Bean
public MessageChannel outputObject() {
return new DirectChannel();
}
@Bean
public Jackson2JsonObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return new Jackson2JsonObjectMapper(mapper);
}
@Bean
@Transformer(inputChannel = "input", outputChannel = "inputObject")
public JsonToObjectTransformer jsonToObjectTransformer() {
return new JsonToObjectTransformer(someTargetClass.class, jackson2JsonObjectMapper);
}
@Bean
@Transformer(inputChannel = "outputObject", outputChannel = "output")
public ObjectToJsonTransformer objectToJsonTransformer() {
return new ObjectToJsonTransformer(jackson2JsonObjectMapper);
}
}
Then in another class I have something like this:
public class AnotherClass {
@Transformer(inputChannel = "inputObject", outputChannel = "outputObject")
public Message<someTargetClass> transform(Message<someTargetClass> incoming) {
//Here I'm getting Caused by: org.springframework.messaging.MessageHandlingException: nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to com.whatever.someTargetClass
SomeTargetClass messagePayload = incoming.getPayload();
}
}
So the flow is input->inputObject-outputObject->output
But somehow I'm getting a classCastException as Input payload coming as a String and not as a Java Object (someTargetClass type).
Caused by: org.springframework.messaging.MessageHandlingException: nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to com.whatever.someTargetClass at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:96) ~[spring-integration-core-4.2.5.RELEASE.jar:na] at org.springframework.integration.transformer.AbstractMessageProcessingTransformer.transform(AbstractMessageProcessingTransformer.java:90) ~[spring-integration-core-4.2.5.RELEASE.jar:na] at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:89) ~[spring-integration-core-4.2.5.RELEASE.jar:na] ... 52 common frames omitted
So, Somehow looks like the flow through the channels is not working properly. What am I missing?
My theory is that the source is producing a string, the fact that is json is not being transmitted, when the transformer receives the string can not convert it to SomeTargetClass. If this is true, this should fix it:
In the transform method change:
SomeTargetClass messagePayload = incoming.getPayload();
to :
String jsonString = incoming.getPayload();
SomeTargetClass messagePayload = (SomeTargetClass) JSON.std.beanFrom(SomeTargetClass.class, jsonString);
This is using Jackson Jr, but I am sure you can find the replacement in Jackson.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.