Adam Bien's Weblog

JSON Is The New Data Transfer Object (DTO)

The JSON processing API comes with Java EE 7 and is already integrated with JAX-RS. JsonObject and JsonArray are serialized and deserialized by the JAX-RS runtime without any additional effort.

JsonObject is a Map<String, JsonValue> and so a generic and dynamic DTO.

Because the entities know their state and also have access to private data, the JSON-mapping happens directly within the domain objects:

public class Workshop {

    private String name;
    private int duration;

    public Workshop(String name, int duration) { = name;
        this.duration = duration;

    public Workshop(JsonObject input) { = input.getString("name");
        this.duration = input.

    public JsonObject toJson() {
        return Json.createObjectBuilder().
                add("duration", this.duration).

Now the JAX-RS resource class only has to invoke the entities method to map from and to the JsonObject representation:

public class WorkshopsResource {

    RegistrationStore store;

    public JsonArray all() {
        JsonArrayBuilder list = Json.createArrayBuilder();
        List<Workshop> all =;;

    public void save(JsonObject input) { Workshop(input));
See you at Java EE Microservices. Is Munich's airport too far? Learn from home:

NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices


A book about rethinking Java EE Patterns


Hi Adam,

This is a nice, neat solution for using JSON in Java EE 7 and I am very much a fan of your "no libraries" ethos however in this case wouldn't it be worth it to use one of the many popular, stable JSON binding frameworks? For example jackson or Gson?

Of course, JSON-B will solve this problem in Java EE 8 but until then isn't a JSON binding library the one dependency that is probably worth breaking the rule for?


Posted by Daniel Banks on February 06, 2017 at 09:30 AM CET #

That is good. How about serializing/deserializing date types?

Posted by Nazir Bunu on February 06, 2017 at 04:30 PM CET #

Hi Adam,

I like the "DTOless" approach, but I never found how I could reconcile this pattern with tools like Swagger.

Any ideas?

Posted by Mathieu Lachance on February 07, 2017 at 03:18 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments