我正在尝试在解组 bean 对象后进行 api 调用并获取有效负载并丰富内容。我可以看到已经发生了对绑定对象的解组,但是在进行 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:
转换器路由.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();
}
下面我已经实现了聚合策略。在解组 newExchange api返回的有效负载后,它将获得 oldExchange。
我的处理器.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;
}
}