gpt4 book ai didi

java - 无法从 START_OBJECT token 中反序列化 的实例

转载 作者:行者123 更新时间:2023-12-01 18:44:35 30 4
gpt4 key购买 nike

我面临着 Spring 和 Jackson 的问题。我试图从 this API 获取输入。我使用 http://www.jsonschema2pojo.org/ 创建了 Java 模型

型号为


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();

}

当我运行代码并尝试获取地址“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]

最佳答案

感谢@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();

}

为了获取地震发生地点的数组,我刚刚在要素类中验证了方法

 @Override
public String toString() {
return properties.getPlace();
}

谢谢大家的帮助。我得到了我需要的东西。

现在我想从前端映射参数以将其提供给我的后端,而不是硬编码的字符串。

关于java - 无法从 START_OBJECT token 中反序列化 <DataModel> 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865461/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com