Customizing naming strategies
Rather than configuring the property name in the JSON string for each attribute specifically, you can define a general naming strategy globally to be applied on all the JSON attributes. This is done by using the property naming strategy option in the jsonb
configuration object, while creating the jsonb
object. Let's see an example of this:
public class Movie { private long id; private String title; private int productionYear; // getters and setters here } JsonbConfig config = new JsonbConfig() .withPropertyNamingStrategy (PropertyNamingStrategy.UPPER_CAMEL_CASE) .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);
As you can see in the previous example, we have used the withPropertyNameingStrategy...