[英]Jackson: How to only ignore a Json property when creating a response?
我的Spring
应用程序正在从S3
获取String
,我需要将其转换为JSON
,然后再转换为 Person object。这一切都按预期工作。
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?
我的Object如下:
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 但这没有用。
您可以使用如下代码:
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "balance")
private Integer balance;
或者您可以仅在 getter 方法上添加 @JsonIgnore。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.