pbjerry10
pbjerry10

Reputation: 187

Setter issue while converting JSON to Java Object

I'm trying to convert below simple JSON to Java Object using com.fasterxml.jackson.core. I have problem with bonusAmount field setter method.

JSON:

{"amount":332.5, "bonusamount":3, "action":"Spend"} 

Java class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class GameRequest {

@JsonProperty("amount")
private BigDecimal amount;

@JsonProperty("bonusamount")
private BigDecimal bonusAmount;

@JsonProperty("action")
private String action;

.....

public BigDecimal getBonusAmount() {
    return bonusAmount;
}

public void setBonusAmount(BigDecimal bonusAmount) {
    this.bonusAmount = bonusAmount;
}

Value of bonusAmount field is NULL when I try to use it but if I change name of setter method from setBonusAmount to setBonusamount then it works. Can someone tell me why??

Upvotes: 0

Views: 189

Answers (1)

Halko Karr-Sajtarevic
Halko Karr-Sajtarevic

Reputation: 2268

That is because you have renamed your field using @JsonProperty("bonusamount") that means Jackson searches for a method named setBonusamount (first char toUpperCase, rest stays the same)

Upvotes: 1

Related Questions