Over a million developers have joined DZone.

Hacking With the JSONB 1.0 Public Review [Snippet]

This article gives a quick example of JSR 367, the JSONB spec.

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

The Public Review of JSR 367 (JSONB 1.0) has just recently been announced. The snapshot version can already be tested.

For an example see the JSONB module of my JAX-RS Hypermedia project.

As JSONB — provided by the reference implementation — can’t obviously be integrated in JAX-RS yet, we need some boilerplate code in the JAX-RS resource methods.

See the CartResource class as an example:

public class CartResource {


    // JAX-RS 2.0 obviously isn't integrated with JSONB yet
    private Jsonb jsonb;

    // as deserialization is done manually, validation also isn't integrated by JAX-RS
    Validator validator;

    public void initJsonb() {
        jsonb = JsonbBuilder.create();

    public StreamingOutput getShoppingCart() {
        final ShoppingCart cart = shoppingCart.getShoppingCart();

        return output -> jsonb.toJson(cart, output);

    public void addItem(InputStream input) {
        final CartInsertion insertion = jsonb.fromJson(input, CartInsertion.class);

        final Set<ConstraintViolation<CartUpdate>> violations = validator.validate(insertion);
        if (!violations.isEmpty())


This shows us one of the most important benefits of using Java EE: being able to seamlessly integrate various specifications — without these extra efforts of course — as soon as the new specs are final and incorporated.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

jsonb ,jsr 367 ,jax-rs

Published at DZone with permission of Sebastian Daschner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}