Customizing property names
As shown earlier, the default behavior of JSON serialization is to use the attribute names as they're found in the class, as the attribute names of the resultant JSON object. It's very common that you may need to change the resulting property name, according to the project's specification. Therefore, the @JsonbProperty
annotation is available to be used on class fields, in order to provide custom names for the annotated attributes in the resulting JSON string.
Let's try customizing the property name of the movie's title in the following example:
public class Movie { private long id; @JsonbProperty("movie-title") private String title; // getters and setters 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"); String json = jsonb.toJson(movie); System.out.println(json);
By running the previous...