我的Spring應用程式正在String從中獲取S3,我需要將其轉換為JSON然后再轉換為 Person 物件。這一切都按預期作業。
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(s );
Person person = mapper.treeToValue(actualObj, Person.class);
if(person.getBalance()>0{
person.setInCredit(true);
}
// todo - how to not return balance?
我的物件如下:
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person{
@JsonProperty("id")
private Integer id;
@JsonIgnore
@JsonProperty("balance")
private Integer balance;
@JsonProperty("inCredit")
private Boolean inCredit;
// other fields and setters etc
}
如上所示,我最初需要讀取余額以確定 inCredit 欄位,但我想從 json 回應中排除余額。
如何確保欄位余額從我的查詢中讀取正常,但不會在我的端點回應中再次回傳?
注意 - 我嘗試添加 JsonIgnore 但這沒有用。
uj5u.com熱心網友回復:
您可以使用如下代碼:
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "balance")
private Integer balance;
或者您可以僅在 getter 方法上添加 @JsonIgnore。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537216.html
