Ignoring properties
In some cases, you may need to ignore some Java object properties from being displayed in the resulting JSON string; they may be non-primary properties holding temporary values, or some internal attributes that will not be interesting to the JSON consumer party. Hence, the @JsonbTransient
annotation is used to hide some Java attributes from being shown in the resulting JSON, as shown in the following examples:
public class Movie { private long id; private String title; @JsonbTransient private int productionYear; // setters and getters here } JsonbConfig config = new JsonbConfig().withFormatting(true); Jsonb jsonb = JsonbBuilder.create(config); Movie movie = new Movie(); movie.setId(15); movie.setTitle("Beauty and The Beast"); movie.setProductionYear(2017); String json = jsonb.toJson(movie); System.out.println(json);
By running the previous example, the output will be as follows:
{
"id": 15,
"title": "Beauty and The Beast...