【问题标题】:Single java field gets mapped to two JSON fields with jackson单个 java 字段使用杰克逊映射到两个 JSON 字段
【发布时间】:2019-11-20 04:05:41
【问题描述】:

POJO 中有一个 isCase 字段,它同时映射到 case 和 is_case(这不是预期的)。 删除 @JsonProperty 注释会删除 json 中的 is_case 字段,但 is_case 是它应该是的。 将 @JsonIgnore 放在 isCase 上并不能消除响应 json 中的大小写。 将 isCase 重命名为 is_case 确实消除了响应 json 中的大小写,但这似乎不太正确。 为什么杰克逊会将单个 java 字段映射到两个字段? POJO 声明如下:

public class CreateRuleResponse {
    @JsonProperty(value = "rule_name", required = true)
    private String name;

    @JsonProperty("rule_num")
    private String ruleNum;

    @JsonProperty(value = "rule_category")
    private RuleCategory ruleCategory;

    @JsonProperty(value = "rule_status")
    private RuleStatus ruleStatus;

    @JsonProperty(value = "rule_channel_type")
    private String ruleChannelType;

    @JsonProperty("action")
    private RuleAction ruleAction;

    @JsonProperty("is_case")
    private Boolean isCase;

    public CreateRuleResponse() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRuleNum() {
        return ruleNum;
    }

    public void setRuleNum(String ruleNum) {
        this.ruleNum = ruleNum;
    }

    public RuleCategory getRuleCategory() {
        return ruleCategory;
    }

    public void setRuleCategory(RuleCategory ruleCategory) {
        this.ruleCategory = ruleCategory;
    }

    public RuleStatus getRuleStatus() {
        return ruleStatus;
    }

    public void setRuleStatus(RuleStatus ruleStatus) {
        this.ruleStatus = ruleStatus;
    }

    public String getRuleChannelType() {
        return ruleChannelType;
    }

    public void setRuleChannelType(String ruleChannelType) {
        this.ruleChannelType = ruleChannelType;
    }

    public RuleAction getRuleAction() {
        return ruleAction;
    }

    public void setRuleAction(RuleAction ruleAction) {
        this.ruleAction = ruleAction;
    }

    public Boolean getCase() {
        return isCase;
    }

    public void setCase(Boolean aCase) {
        isCase = aCase;
    }
}

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    它正在映射变量以及 getter 方法。

    【讨论】:

    • 但是为什么这不会发生在其他所有领域呢?
    • 因为 isCase 是名为 "case" 的布尔值的 getter 方法的常规名称。现在您有一个由注释定义的 JSON 属性“isCase”,还有 JSON 属性“case”,因为库推断您的 isCase() 方法用于访问名为“case”的布尔值。对于其他字段,注释中的属性名称与 getter 函数中隐含的属性名称相同,因此每个变量只有一个不同的属性名称可供显示。
    • 把getRuleAction()的名字改成getPoop()看看会发生什么。
    • 按照惯例,isCase 的 getter 应该是 isIsCase(),因为它是一个 boolean 。或者您可以将变量 isCase 更改为“case”并将 getter 命名为 isCase() 以匹配它们。
    • 这是有道理的,但同时有一个带有getter getRuleAction的ruleAction属性,并且只用“action”进行注释,难道rule_action属性也出现在json中没有意义,因为吸气剂方法?或者 getRuleAction 是 ruleAction 属性的常规名称这一事实是否使得 rule_action 字段不会弹出?
    【解决方案2】:

    将此 getter 方法从 getCase() 重命名为 getIsCase()(遵循约定)。 Jackson 将其视为 2 个不同的字段,因为 getter 和字段名称不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多