EmberTraveller
EmberTraveller

Reputation: 345

Single java field gets mapped to two JSON fields with jackson

There is a isCase field in POJO that gets mapped to case and is_case at the same time(which is not what expected). Removing @JsonProperty annotation gets rid of is_case field in json, but is_case is what it's supposed to be. Putting @JsonIgnore on isCase doesn't get rid of case in response json. Renaming isCase to is_case does get rid of case in response json, but that doesn't seem too right. Why would Jackson ever map single java field to two fields? A POJO is declared like this:

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;
    }
}

Upvotes: 1

Views: 332

Answers (2)

Mushif Ali Nawaz
Mushif Ali Nawaz

Reputation: 3866

Rename this getter method from getCase() to getIsCase() (following the convention). Jackson is treating it as 2 different fields because the getter and the field name do not match.

Upvotes: 1

LowKeyEnergy
LowKeyEnergy

Reputation: 652

It's mapping the variables and also the getter methods.

Upvotes: 1

Related Questions