JSON-B Support

The WebServer supports the JSON-B specification. When this support is enabled, Java objects will be serialized to and deserialized from JSON automatically using Yasson, an implementation of the JSON-B specification.

Maven Coordinates

Declare the following dependency in your project:

<dependency>
    <groupId>io.helidon.media.jsonb</groupId>
    <artifactId>helidon-media-jsonb-server</artifactId>
</dependency>
Copied

Usage

To enable JSON-B support, first create and register a JsonBindingSupport instance with a Routing.Builder. JsonBindingSupport is a Service, so it will install its own Handler that will provide serialization and deserialization services using Yasson, an implementation of the JSON-B specification.

Create and register JsonBindingSupport first
final JsonBindingSupport jsonBindingSupport = JsonBindingSupport.create(); 
final Routing.Builder routingBuilder = Routing.builder();
routingBuilder.register(jsonBindingSupport); 
Copied
  • Create a JsonBindingSupport instance. This instance may be reused freely.
  • Register that JsonBindingSupport instance to enable automatic deserialization of Java objects from and serialization of Java objects to JSON.

Now that automatic JSON serialization and deserialization facilities have been set up, you can register a Handler that works with Java objects instead of raw JSON. Deserialization from and serialization to JSON will be handled according to the JSON-B specification.

Suppose you have a Person class that looks like this:

Hypothetical Person class
public class Person {

    private String name;

    public Person() {
        super();
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}
Copied

Then you can set up a Handler like this:

A Handler that works with Java objects instead of raw JSON
final Routing routing =
    routingBuilder.post("/echo", 
                        Handler.create(Person.class, 
                                       (req, res, person) -> res.send(person)))) 
    .build();
Copied
Example of posting JSON to the /echo endpoint
curl --noproxy '*' -X POST -H "Content-Type: application/json" \
    http://localhost:8080/echo -d '{"name":"Joe"}'
{"name":"Joe"}
Copied