Jackson-databind: @jsonignore annotation working only on field, but not on get/set methods

Created on 3 Oct 2012  路  4Comments  路  Source: FasterXML/jackson-databind

This issue is related to
https://github.com/FasterXML/jackson-annotations/issues/5

... but has been re-created per instructions from Tatu as a jackson-databind issue using a simple non-JAXB non-mixin POJO test-case.

The POJO has a 'userPassword' field along with getter and setter. There is a jsonproperty annotation on the field to convert it from userPassword to user_password.

I cannot get @jsonignore annotation to work just on the getter but still allow setter. I have tried various combinations of annotations on the getter, setter and field.

The test case leverages version 2.0.6.
It is linked below:
https://docs.google.com/open?id=0Bxy6NL7ud-h6NHJ5VkdXUUlYbnM

Here is the java class extracted from the test case zip

package com.test.model;

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

public class Pod
{
    protected String username;

    @JsonProperty(value = "user_password")
    protected java.lang.String userPassword;


    public String getUsername()
    {
        return username;
    }

    public void setUsername(String value)
    {
        this.username = value;
    }

    @JsonIgnore
    @JsonProperty(value = "user_password")
    public java.lang.String getUserPassword()
    {
        return userPassword;
    }

    @JsonProperty(value = "user_password")
    public void setUserPassword(java.lang.String value)
    {
        this.userPassword = value;
    }

}

All 4 comments

First guess (will verify with a test): remove annotation from field. It is annotated to be used, and fields always work as getter/setter, if necessary. Renaming is not needed, since as per Jackson 1.9, implied names are first used for linking, so binding is done first, and renaming will affect the whole "logical" property.

Verified: just remove the @JsonProperty annotation from field, and things work the way you want.
I added a unit test to ensure that deserialization still works (it does).

Thanks Tatu.... it worked!

Here is the revised class that prevents password from being serialized out to JSON (but accepts a password when unmarshalling from JSON)

package com.test.model;

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

public class Pod
{
    protected String username;

    protected java.lang.String userPassword;

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String value)
    {
        this.username = value;
    }

    @JsonIgnore
    @JsonProperty(value = "user_password")
    public java.lang.String getUserPassword()
    {
        return userPassword;
    }

    @JsonProperty(value = "user_password")
    public void setUserPassword(java.lang.String value)
    {
        this.userPassword = value;
    }

}

Ok good. Btw, forgot to mention that @JsonProperty is also redundant in 'getUserPassword()'. Just mentioning this for completeness sake.

Thank you for verifying this.

Was this page helpful?
0 / 5 - 0 ratings