Jordan Robinson Jordan Robinson - 7 months ago 40
Java Question

Why is Jackson mapping these values twice, in differing case?

I'm mapping a Java object to JSON using Jackson, the object is a pretty simple pojo class that looks like this:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Area {
public int Id;
public String Name;

public Area() {
Name = "";

public int getId() {
return Id;

public void setId(int id) {
Id = id;

public String getName() {
return Name;

public void setName(String Name) {
this.Name = Name;

The mapping code then looks like this:

ObjectMapper mapper = new ObjectMapper();


areaJSON = mapper.writeValueAsString(area);

But the value of areaJSON at this point is then as follows:


Note the multiple values with differing case.

What am I doing wrong?


Jackson thinks that the fields Id and Name are different properties from the ones returned by the getters because the case is different. Using standard JavaBeans naming conventions, Jackson infers the fields corresponding to the getters are named id and name, not Id and Name.

tl;dr case matters.

There are two simple ways to fix this problem:

  1. Remove the @JsonAutoDetect annotation from this class entirely. I'm pretty sure that the default annotation values are taking precedence over the ObjectMapper's configuration. Alternately:

  2. Don't muck with the ObjectMapper at all. Change the @JsonAutoDetect on the class to

    @JsonAutoDetect( fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, creatorVisibility = Visibility.NONE )