Apache Camel 将 JSON 转换为 POJO

Apache Camel transform JSON to POJO

我正在尝试将一个简单的 JSON 文件转换为 Apache Camel 中的 POJO,但我无法让它工作。

Routes.java

package org.acme.bindy.ftp;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.BindyType;
import org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.model.dataformat.JsonDataFormat;
import org.apache.camel.Processor;


public class Routes extends RouteBuilder {

    @Override
    public void configure() throws Exception {        
        from("file:{{json.temp}}?delay=1000")   
        .unmarshal().json(JsonLibrary.Jackson, WeatherForecast[].class)     
        .to("file:{{json.processed}}"); 
        ....
        ....
    }
}

WeatherForecast.java

package org.acme.bindy.ftp;

import java.util.Objects;

import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonIgnore;


public class WeatherForecast {
    @JsonProperty("date")
    private String date;
    @JsonProperty("temperatureC")
    private int temperatureC;
    @JsonProperty("temperatureF")
    private int temperatureF;
    @JsonProperty("summary")
    private String summary;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getTemperatureC() {
        return temperatureC;
    }

    public void setTemperatureC(int temperatureC) {
        this.temperatureC = temperatureC;
    }

    public int getTemperatureF() {
        return temperatureF;
    }

    public void setTemperatureF(int temperatureF) {
        this.temperatureF = temperatureF;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}

天气预报-json

[{"date":"2022-05-07T13:49:22.0517663+02:00","temperatureC":36,"temperatureF":96,"summary":"Mild"},{"date":"2022-05-08T13:49:22.052558+02:00","temperatureC":21,"temperatureF":69,"summary":"Mild"},{"date":"2022-05-09T13:49:22.0525661+02:00","temperatureC":13,"temperatureF":55,"summary":"Hot"},{"date":"2022-05-10T13:49:22.0525665+02:00","temperatureC":-2,"temperatureF":29,"summary":"Warm"},{"date":"2022-05-11T13:49:22.0525668+02:00","temperatureC":49,"temperatureF":120,"summary":"Scorching"}]

我收到的错误是: 没有可用类型的正文:java.io.InputStream 但具有类型:org.acme.bindy.ftp.WeatherForecast[] on:weather.json。原因:没有类型转换器可用于将类型:org.acme.bindy.ftp.WeatherForecast[] 转换为所需类型:java.io.InputStream。交换[]。原因:[org.apache.camel.NoTypeConversionAvailableException - 没有类型转换器可用于将类型:org.acme.bindy.ftp.WeatherForecast[] 转换为所需类型:java.io.InputStream]

如有任何帮助,我们将不胜感激

错误似乎发生在 (成功)解组到 WeatherForecast 类 数组之后,此时需要获取 POJO 数组写入文件系统:

.unmarshal().json(JsonLibrary.Jackson, WeatherForecast[].class)     
.to("file:{{json.processed}}"); 

Camel 希望转换器将您的主体(bean 数组)转换为输入流以便能够将其写入磁盘,这并不奇怪

...to convert from type: org.acme.bindy.ftp.WeatherForecast[] to the required type: java.io.InputStream