Apache Camel将JSON转换为POJO

au9on6nz  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(177)

我尝试将一个简单的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"}]

**我收到的错误是:**没有可用类型的正文:输入流,但具有类型:天气预报[],网址:weather.json.引起原因:没有类型转换器可用于从类型转换:[]转换为所需类型:交换[]。导致原因:[org.apache.camel.NoTypeConversionAvailableException -没有类型转换器可用于从类型:[]转换为所需类型:输入数据流]

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

htrmnn0y

htrmnn0y1#

该错误似乎发生在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

相关问题