Я пытаюсь сделать вызов API, получить полезную нагрузку и обогатить содержимое после демаршалирования объекта bean. Я вижу, что демаршаллинг объекта bindy уже произошел, но он вызывает приведенное ниже исключение после вызова API.
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route1 ] [route1 ] [file://inbox?fileName=3100.txt ] [ 4]
[route1 ] [unmarshal1 ] [unmarshal[org.apache.camel.model.DataFormatDefinition@6e255e77] ] [ 0]
[route1 ] [enrich1 ] [enrich[constant{direct:getUN}] ] [ 0]
[route2 ] [setHeader1 ] [setHeader[CamelHttpMethod] ] [ 0]
[route2 ] [setHeader2 ] [setHeader[Content-Type] ] [ 0]
[route2 ] [to2 ] [http://localhost:8084/get/45718 ] [ 1]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: PART_INB_IFD(ctrl_seg=CTRL_SEG(TRNNAM=PART_TRAN, TRNVER=9.0, WHSE_ID=----, PART_SEG=PART_SEG(SEGNAM=PART, TRNTYP=R, PRT_CLIENT_ID=----, PRTNUM=598, HAZMAT_FLG=1, VC_TOSG_HAZ_UN_NUM=null))) of type: com.model.PART_INB_IFD on: Message[]. Caused by: No type converter available to convert from type:
ConverterRoute. java
@Override
public void addRoutesToCamelContext(CamelContext context) throws Exception {
context.addRoutes(new RouteBuilder() {
public void configure() {
try {
XmlFriendlyNameCoder nameCoder = new XmlFriendlyNameCoder("_-", "_");
Dom4JDriver dom4JDriver = new Dom4JDriver(nameCoder);
xStreamDataFormat.setAliases(Collections.singletonMap("PART_INB_IFD", PART_INB_IFD.class.getCanonicalName()));
xStreamDataFormat.setXstreamDriver(dom4JDriver);
from(SOURCE_INPUT_PATH).
log("Received input from file and body is ${body}").
split().tokenize(System.lineSeparator()).
unmarshal(bindyBeanConfig.bindyCsvDataFormat()).
enrich("direct:getUN", new MyProcessor()).
marshal(xStreamDataFormat).
log("After Marshalling and body is ${body}").
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").
end();
from("direct:getUN")
.setHeader(Exchange.HTTP_METHOD,constant("GET"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8084/get/45718")
.log("HTTP response status: ${header.CamelHttpResponseCode}")
.log(LoggingLevel.DEBUG, "HTTP response body:\n${body}");
} catch (Exception e) {
e.printStackTrace();
}
}
});
context.suspend();
context.stop();
}
Ниже я реализовал стратегию агрегирования. Который получит oldExchange после демаршалинга newExchange полезной нагрузки, которую возвращает API.
MyProcessor. java
public class MyProcessor implements AggregationStrategy {
private static final Logger logger = LoggerFactory.getLogger(MyProcessor.class);
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
PART_INB_IFD originalBody = oldExchange.getIn().getBody(PART_INB_IFD.class);
String resourceResponse = newExchange.getIn().getBody(String.class);
logger.info("After Unmarshalling Original Body {}", originalBody);
logger.info("ResourceResponse {}", resourceResponse);
return oldExchange;
}
}