1

I have a class Student with some fields. I wanted to give custom names for the JSON fields that get returned.

public class Student {


    @JsonProperty("name")
    private String mName;

    @JsonProperty("DOB")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private Date mBirthDate;

    @JsonProperty("SSN")
    private String mSocialSecurityNumber;

    public Student() {
    }

    public Student(String mName, Date mBirthDate, String mSocialSecurityNumber) {
        this.mName = mName;
        this.mBirthDate = mBirthDate;
        this.mSocialSecurityNumber = mSocialSecurityNumber;
    }


    public String getName() {
        return mName;
    }

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

    public Date getBirthDate() {
        return mBirthDate;
    }

    public void setBirthDate(Date mBirthDate) {
        this.mBirthDate = mBirthDate;
    }

    public String getSocialSecurityNumber() {
        return mSocialSecurityNumber;
    }

    public void setSocialSecurityNumber(String mSocialSecurityNumber) {
        this.mSocialSecurityNumber = mSocialSecurityNumber;
    }
}

My JSON output has both the raw field name (based on the getter name, e.g. getSocialSecurityNumber()), as well as the name specified in my @JsonProperty attributes.

It seems like if I move the @JsonProperty attributes to the getters, then I don't get the doubleup of the fields. Is there not a way I can do this by just having the annotations on the fields, which I feel is a little cleaner?

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176

1 Answers1

5

Configure the ObjectMapper to consider only the fields:

ObjectMapper mapper = new ObjectMapper();    
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

In Spring Boot you can use Jackson2ObjectMapperBuilder to configure the ObjectMapper:

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {

    return new Jackson2ObjectMapperBuilder() {

        @Override
        public void configure(ObjectMapper objectMapper) {
            super.configure(objectMapper);
            objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
            objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        }
    };
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Not familiar with ObjectMapper -- not using it. This is happening all under the hood for me via Spring Boot 1.4.3 – Stealth Rabbi Jan 28 '17 at 02:09
  • @StealthRabbi So you can use `Jackson2ObjectMapperBuilder`. Let me provide you with an example. – cassiomolin Jan 28 '17 at 02:11
  • Looks like I can manipulate the bean as shown here: http://stackoverflow.com/questions/28324352/how-to-customise-the-jackson-json-mapper-implicitly-used-by-spring-boot – Stealth Rabbi Jan 28 '17 at 02:14
  • 1
    This is helpfu. So, the default is presumbably PropertyAccessor.GETTER, which explains why that, without @JsonProperty tags, my JSON fields are 'name' and not 'mName'. – Stealth Rabbi Jan 28 '17 at 02:19