当前位置: 首页 > 知识库问答 >
问题:

无法从START_OBJECT标记反序列化实例

薛弘壮
2023-03-14

package io.github.mat3e.earthquake.jsonObject;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "type",
    "metadata",
    "features",
    "bbox"
})
public class DataModel {

    @JsonProperty("type")
    private String type;
    @JsonProperty("metadata")
    private Metadata metadata;
    @JsonProperty("features")
    private List<Feature> features = null;
    @JsonProperty("bbox")
    private List<Double> bbox = null;

    @JsonProperty("type")
    public String getType() {
        return type;
    }

    @JsonProperty("type")
    public void setType(String type) {
        this.type = type;
    }

    @JsonProperty("metadata")
    public Metadata getMetadata() {
        return metadata;
    }

    @JsonProperty("metadata")
    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }

    @JsonProperty("features")
    public List<Feature> getFeatures() {
        return features;
    }

    @JsonProperty("features")
    public void setFeatures(List<Feature> features) {
        this.features = features;
    }

    @JsonProperty("bbox")
    public List<Double> getBbox() {
        return bbox;
    }

    @JsonProperty("bbox")
    public void setBbox(List<Double> bbox) {
        this.bbox = bbox;
    }

}

当然,所有依赖项(子类追加在同一个字符串中)。

从外部API获取数据的代码是;

@GetMapping("/3")
    public String getCostam() {
        RestTemplate rest = new RestTemplate();

        ResponseEntity<DataModel[]> responseEntity = restTemplate.getForEntity(url2, DataModel[].class);
        Object[] objects = responseEntity.getBody();
        MediaType contentType = responseEntity.getHeaders().getContentType();
        HttpStatus statusCode = responseEntity.getStatusCode();
        return statusCode.toString();

    }

当我运行代码并试图获取adress“API/3”时,出现以下错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `[Lio.github.DataModel;` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]

共有1个答案

沈良策
2023-03-14

感谢@Willem和@Mkarasik的帮助:

我在代码中做了以下更改

@GetMapping("/3")
    public String getCostam() {
        RestTemplate rest = new RestTemplate();

        ResponseEntity<DataModel> responseEntity = restTemplate.getForEntity(url2, DataModel.class);

        MediaType contentType = responseEntity.getHeaders().getContentType();
        HttpStatus statusCode = responseEntity.getStatusCode();

        System.out.println(responseEntity
                .getBody().getFeatures());



        return statusCode.toString();

    }

为了得到地震发生的地方数组,我只是在特征类中ovverided方法

 @Override
    public String toString() {
        return properties.getPlace();
    }
 类似资料: