com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 无法识别的字段 "Symbol"
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Symbol"
我正在尝试拍摄一些看起来像这样的 JSON(来自 AlphaVantage):
{
"Symbol": "IBM",
"AssetType": "Common Stock",
"Name": "International Business Machines Corporation",
... more properties
}
并使用 Jackson (& Ebeans) 解析它:
ObjectMapper objectMapper = new ObjectMapper();
String json = response.body().string();
Stock stock = objectMapper.readValue(json, Stock.class);
stock.save();
我的库存 class 看起来像这样:
@Entity
@Table(name = "stock")
public class Stock extends Model {
@Id
private Long id;
private String Symbol;
private String AssetType;
private String Name;
... other properties
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getSymbol() {
return this.Symbol;
}
public void setSymbol(String Symbol) {
this.Symbol = Symbol;
}
... other setters/getters
}
相反,我收到以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Symbol" (class com.myprojectname.Stock), not marked as ignorable (60 known properties: "ebitda", "sharesOutstanding", "bookValue", ....more fields
为什么 Jackson 在连接到我的 Stock class 时遇到问题?如何将 JSON 中的符号连接到股票 class 中的符号?
编辑:如果我将符号更改为小写,我会收到相同的错误消息:
@Entity
@Table(name = "stock")
public class Stock extends Model {
@Id
private Long id;
private String symbol;
public String getSymbol() {
return this.symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
将 属性 的名称从 Symbol
更改为 symbol
无济于事,因为 json 文件中仍然有 "Symbol"
。
您可以尝试使用@JsonProperty
注解like in the example。对于 "Symbol"
json 字段,它可能如下所示:
//...
private String Symbol;
@JsonProperty("Symbol")
public String getSymbol() {
return this.Symbol;
}
@JsonProperty("Symbol")
public void setSymbol(String symbol) {
this.Symbol = symbol;
}
// ...
这种方法也适用于与 json 对应的大写/小写字母不同的其他字段。
编辑:就像我上面评论中链接的问题 the answer 一样 - 解释一下:
Since your setter method is named setSymbol()
Jackson assumes the
variable is named symbol
because of the Java naming conventions
(variables should start with lower case letters).
edit(2):其他选项将利用 the object mapper configuration. So instead of the annotations approach you could use the approach from here 并使映射器不区分大小写:
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
其他属性我觉得有用的是:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
在上面链接的 Baeldung 文章中有描述
您还可以将 ObjectMapper
配置为仅检查字段并忽略 getter:
JsonMapper mapper = JsonMapper.builder()
.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
.build();
- How to specify jackson to only use fields - preferably globally
我正在尝试拍摄一些看起来像这样的 JSON(来自 AlphaVantage):
{
"Symbol": "IBM",
"AssetType": "Common Stock",
"Name": "International Business Machines Corporation",
... more properties
}
并使用 Jackson (& Ebeans) 解析它:
ObjectMapper objectMapper = new ObjectMapper();
String json = response.body().string();
Stock stock = objectMapper.readValue(json, Stock.class);
stock.save();
我的库存 class 看起来像这样:
@Entity
@Table(name = "stock")
public class Stock extends Model {
@Id
private Long id;
private String Symbol;
private String AssetType;
private String Name;
... other properties
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getSymbol() {
return this.Symbol;
}
public void setSymbol(String Symbol) {
this.Symbol = Symbol;
}
... other setters/getters
}
相反,我收到以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Symbol" (class com.myprojectname.Stock), not marked as ignorable (60 known properties: "ebitda", "sharesOutstanding", "bookValue", ....more fields
为什么 Jackson 在连接到我的 Stock class 时遇到问题?如何将 JSON 中的符号连接到股票 class 中的符号?
编辑:如果我将符号更改为小写,我会收到相同的错误消息:
@Entity
@Table(name = "stock")
public class Stock extends Model {
@Id
private Long id;
private String symbol;
public String getSymbol() {
return this.symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
将 属性 的名称从 Symbol
更改为 symbol
无济于事,因为 json 文件中仍然有 "Symbol"
。
您可以尝试使用@JsonProperty
注解like in the example。对于 "Symbol"
json 字段,它可能如下所示:
//...
private String Symbol;
@JsonProperty("Symbol")
public String getSymbol() {
return this.Symbol;
}
@JsonProperty("Symbol")
public void setSymbol(String symbol) {
this.Symbol = symbol;
}
// ...
这种方法也适用于与 json 对应的大写/小写字母不同的其他字段。
编辑:就像我上面评论中链接的问题 the answer 一样 - 解释一下:
Since your setter method is named
setSymbol()
Jackson assumes the variable is namedsymbol
because of the Java naming conventions (variables should start with lower case letters).
edit(2):其他选项将利用 the object mapper configuration. So instead of the annotations approach you could use the approach from here 并使映射器不区分大小写:
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
其他属性我觉得有用的是:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
在上面链接的 Baeldung 文章中有描述
您还可以将 ObjectMapper
配置为仅检查字段并忽略 getter:
JsonMapper mapper = JsonMapper.builder()
.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
.build();
- How to specify jackson to only use fields - preferably globally