opus111
opus111

Reputation: 2854

@JsonIgnore ignored for public getter

Hello I have read all the posts on this, but nothing seems to help

I have a public getter that points to a circular structure that I do not want serialized into Json. I have looked through the other posts and tried the suggestions but nothing works.

Currently, I am using ObjectMapper @JsonIgnore and @JsonAutoDetect like this:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class Bah {

    @JsonIgnore
    public String getFoo() { return foo; }

However, the getter is still being serialized into my Json structure. But when I rename getFoo to something without get (e.g. foo()) it works, but "public getX()" seems to override the @JsonIgnore

What am I missing? I'm sure its something dumb

Thanks Peter

Upvotes: 4

Views: 2576

Answers (2)

opus111
opus111

Reputation: 2854

OK, I got it, and as predicted it was really dumb!

Turns out I had both com.codehaus.jackson and com.fasterxml.jackson in my project. The ObjectMapper came from fasterxml and the annotations came from codehaus >:-P

Now everything is working as expected as expected. Thank you all for the help. P

Upvotes: 2

Sam Berry
Sam Berry

Reputation: 7844

Try disabling AUTO_DETECT_GETTERS in your ObjectMapper. You can do this wherever you are instantiating ObjectMapper like this:

ObjectMapper om = new ObjectMapper();
om.disable(MapperFeature.AUTO_DETECT_GETTERS);

Or, extend ObjectMapper and use the custom one throughout your project:

public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper() {
        disable(MapperFeature.AUTO_DETECT_GETTERS);
        // more project-wide config
    }
}

Upvotes: 1

Related Questions