geeky · java

Jackson Ignore Property/Convenience Method

Here’s something I learned about Jackson and you may run into it.

What gets serialized by default?

Properties of an object are initially determined by process called auto-detection: all member methods and fields are checked to find:

  • “Getter” methods: all no-argument public member methods which return a value, and conform to naming convention of “getXxx” (or “isXxx”, if return type is boolean; called “is-getter”) are considered to infer existence of property with name “xxx” (where property name is inferred using bean convention, i.e. the leading capitali letter(s) is changed to lower case
  • field properties: all public member fields are considered to represent properties, using field name as is.

This means Jackson will map any convenience methods you create on the POJO that’s public and starts with “get” and returns a value. This may NOT be the behavior you want.

@JsonIgnoreProperties can be used at a class level and it works for me.

But in order to prevent me from tediously adding all of convenience methods on my POJO to the list, I started to use “return” as the keyword instead of “get” so Jackson will ignore those methods by default. Of course if you want JSTL to be able to show the convenience method output on the screen, the convenience method has to begin with “get”. So I’m using a mix of solutions to this problem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s