William Lieurance's Tech Blog

@JsonIgnore vs @JsonbTransient


In new EE app servers, Jackson-specific @JsonIgnore is deprecated in favor of JSON-B's @JsonbTransient

I was trying to remove some unnecessary fields from a JSON response coming out of an app in Payara 5 the other day, but no annotation I was putting on my entity classes seemed to do anything. I tried @JsonIgnore, @JsonProperties, and even some weird @JsonView overrides. It didn't matter if the annotations were on the fields, the getters and setters, or even on the classes themselves. After iterating in vain for a while, I realized that if all of those annotations were not having an effect, it probably wasn't that I was misusing them so much as the appserver just wasn't paying attention to the library that provided them.

That kind of investigation has been something I had to learn over the course of my career. One of the first things I've had to force myself to do when investigating a problem where a chunk of code seemingly doesn't do what I expect is to make sure that it's running at all. Frequently, I'll find that I had misunderstood the flow of the program or missed a hook somewhere, and my code just straight-up isn't being called. Only after that can I worry about fixing the errors that are surely in that block of code that had never run.

In this case, the fix is simple but non-trivial. First, I import the JSON-B API dependency into my pom.xml file.

    <!-- JSON-B API -->

Then, instead of @JsonIgnore from the Jackson library I use the @JsonbTransient annotation and let the app server use whatever implementation of JSON-B that it happens to like.

NOTE Make sure you include the b in the middle of @JsonbTransient. I lost a frustrating hour to missing that little letter.

import javax.json.bind.annotation.JsonbTransient;

public class SomeEntity {

    private String publicWebData;

    private String databaseOnlyData;

    /* getters and setters here */

Note as well in the 2.0 version of the bind-api, the namespace has changed from javax. to jakarta.. You should check and see which version your app server supports.

Check out the additional annotations in the spec over at http://json-b.net/docs/user-guide.html. They seem to have covered all my use cases at least.