亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Apache Camel - 在 Rest DSL 中編組到 Json

Apache Camel - 在 Rest DSL 中編組到 Json

SMILET 2023-06-14 16:38:58
我正在嘗試使用 Camel 中的 Rest DSL 處理一個 csv 文件。當我將 CSV 和 Marshall 拆分為 JSON 時,我遇到了一些奇怪的行為。這是我的代碼:@Componentpublic classProcessHandler extends RouteBuilder {    @Override    protected void defineRoute() throws Exception {        DataFormat csv = new BindyCsvDataFormat(CsvModel.class);        rest("/")                .post().produces("application/json")                .route()                .unmarshal(csv)                .split(body()).parallelProcessing().streaming()                .marshal().json(JsonLibrary.Gson)                .filter().jsonpath("$[?(@.counter==3)]")                .log("${body}")但是我收到錯誤消息:Error during type conversion from type: java.lang.String to the required type: byte[] with value [CsvModel(....但是,如果我按如下方式編輯路線:@Componentpublic classProcessHandler extends RouteBuilder {    @Override    protected void defineRoute() throws Exception {        DataFormat csv = new BindyCsvDataFormat(CsvModel.class);        rest("/")                .post().produces("application/json")                .route()                .unmarshal(csv)                .marshal().json(JsonLibrary.Gson)                .split(body()).parallelProcessing().streaming()                //.filter().jsonpath("$[?(@.counter==3)]")                .log("${body}")它工作正常。然而,我顯然無法正確處理我的消息,因為它被編組為字節表示。如果不使用 Rest DSL,該路由也能正常工作,所以我假設問題出在 http 響應上。如果我嘗試以下操作:我犯了同樣的錯誤。能不能規范化格式,做一些處理步驟,然后返回一個Json?我在某個地方出錯了嗎 - 我很樂意理解為什么會這樣?
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

如果您在內部過濾而不是使用 JsonPath 來實施聚合策略,可能會更容易,因此更容易理解。


實際上,split()默認情況下使用的方法不會給出您期望的結果


這是一個例子:


@Component

public class ProcessHandler extends RouteBuilder {


? ? ? ? ? ? @Override

? ? ? ? ? ? protected void defineRoute() throws Exception {


? ? ? ? ? ? DataFormat csv = new BindyCsvDataFormat(CsvModel.class);


? ? ? ? ? ? rest("/")

? ? ? ? ? ? ? ? ? ? .post().produces("application/json")

? ? ? ? ? ? ? ? ? ? ? ? ?.route()

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .unmarshal(csv)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .split().method(ItemsSplittingStrategy.class, "splitItems")

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .parallelProcessing()

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .marshal().json(JsonLibrary.Gson)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .end()

? ? ? ? ? ? ? ? ? ? .to("file:/file.json");

? ? }

}


我還邀請您檢查可用于拆分器和聚合器及其組合的所有功能。


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號