[英]@JsonProperty present but returning null
I have an annotated Java 11
class that's using Fasterxml
and it's returning null for one of the properties even though I can see the data in the source JSON
. 這個注釋是否有理由不再適用於jackson:2.12.0
?
有問題的屬性在源中大寫,因此在注釋中也大寫。
來源 JSON
{
"stringOne": "hello",
"stringTwo": "world",
"floatOne": 0.0,
"NUM": 15 <-- always returns null
}
日志語句
TestJson{stringOne='hello', stringTwo='world', floatOne=0.0, NUM=null}
Class
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
public class TestJson implements Serializable{
protected String stringOne;
protected String stringTwo;
protected Float floatOne;
protected Integer NUM;
@JsonProperty("stringOne")
public String getStringOne() {
return stringOne;
}
public void setStringOne(String stringOne) {
this.stringOne = stringOne;
}
@JsonProperty("stringTwo")
public String getStringTwo() {
return stringTwo;
}
public void setStringTwo(String stringTwo) {
this.stringTwo = stringTwo;
}
@JsonProperty("floatOne")
public Float getFloat() {
return floatOne;
}
public void setFloat(Float floatOne) {
this.floatOne = floatOne;
}
//////// Returns null for every 'NUM' property
@JsonProperty("NUM")
public Integer getNum() {
return NUM;
}
@JsonProperty("NUM")
public void setNum(Integer NUM) {
this.NUM= NUM;
}
Override
public String toString() {
return "TestJson{" +
"stringOne='" + stringOne + '\'' +
", stringTwo='" + stringTwo + '\'' +
", floatOne=" + floatOne +
", NUM=" + NUM+
'}';
}
}
編輯
將用於檢索/序列化數據的請求添加為TestJson
object
ClientResponse<TestJson> response = null;
TestJson testJson = null;
logger.info(encodeForLog("apiUrl.getPath: " + apiUrl.getPath()));
logger.info("proxy:" + proxy);
try {
response = proxy.getData(apiUrl.getPath());
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
testJson = response.getEntity();
return testJson;
} else {
throw new FailedException("HTTP failure");
}
}
finally {
if (response != null) {
response.releaseConnection();
}
}
在 setter 方法上設置 @JsonProperty。
@JsonProperty("NUM")
public Integer getNum() {
return num;
}
@JsonProperty("NUM")
public void setNum(Integer num) {
this.num = num;
}
可用於將非靜態方法定義為邏輯屬性(取決於其簽名)的“setter”或“getter”的標記注釋,或用作邏輯屬性的非靜態 object 字段(序列化、反序列化)財產。
Jackson版本無所謂:
用這個 class (Jackson 2.11.4) 測試:
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
public class TestJson implements Serializable {
protected String stringOne;
protected String stringTwo;
protected Float floatOne;
protected Integer num;
@JsonProperty("stringOne")
public String getStringOne() {
return stringOne;
}
public void setStringOne(String stringOne) {
this.stringOne = stringOne;
}
@JsonProperty("stringTwo")
public String getStringTwo() {
return stringTwo;
}
public void setStringTwo(String stringTwo) {
this.stringTwo = stringTwo;
}
@JsonProperty("floatOne")
public Float getFloat() {
return floatOne;
}
public void setFloat(Float floatOne) {
this.floatOne = floatOne;
}
//////// Returns null for every 'NUM' property
@JsonProperty("NUM")
public Integer getNum() {
return num;
}
@JsonProperty("NUM")
public void setNum(Integer num) {
this.num= num;
}
@Override
public String toString() {
return "TestJson{" +
"stringOne='" + stringOne + '\'' +
", stringTwo='" + stringTwo + '\'' +
", floatOne=" + floatOne +
", NUM=" + num+
'}';
}
}
Output:
以及 TestJson 的更好解決方案:
public class TestJson implements Serializable {
protected String stringOne;
protected String stringTwo;
protected Float floatOne;
protected Integer num;
@JsonCreator
public TestJson(@JsonProperty("stringOne") String stringOne
,@JsonProperty("stringTwo") String stringTwo
,@JsonProperty("floatOne") Float floatOne
,@JsonProperty("NUM") Integer num) {
this.stringOne = stringOne;
this.stringTwo = stringTwo;
this.floatOne = floatOne;
this.num = num;
}
public String getStringOne() {
return stringOne;
}
public void setStringOne(String stringOne) {
this.stringOne = stringOne;
}
public String getStringTwo() {
return stringTwo;
}
public void setStringTwo(String stringTwo) {
this.stringTwo = stringTwo;
}
public Float getFloat() {
return floatOne;
}
public void setFloat(Float floatOne) {
this.floatOne = floatOne;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num= num;
}
@Override
public String toString() {
return "TestJson{" +
"stringOne='" + stringOne + '\'' +
", stringTwo='" + stringTwo + '\'' +
", floatOne=" + floatOne +
", NUM=" + num+
'}';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.