[Hawkular-dev] generation of message POJOs via JSON schema

Michael Burman miburman at redhat.com
Thu Feb 26 16:16:00 EST 2015


Hi,

I'm all for strong typing the stuff we send out and receive. As for automatic generation, same can be applied by the swagger-codegen, which generates from our documented swagger-annotations the client-code. So it works in a  reversed way, from our Java-code to JSON, like the following:

    "MetricParams" : {
      "id" : "MetricParams",
      "description" : "Metric definition model",
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "tags" : {
          "$ref" : "Map[string,string]"
        },
        "dataRetention" : {
          "type" : "integer",
          "format" : "int32",
          "description" : "Overrides the data retention setting at the tenant level"
        }
      }
    },

And swagger-codegen understands that.

  - Micke

----- Original Message -----
From: "John Mazzitelli" <mazz at redhat.com>
To: hawkular-dev at lists.jboss.org
Sent: Thursday, 26 February, 2015 9:47:06 PM
Subject: [Hawkular-dev] generation of message POJOs via JSON schema

There is this really nice maven plugin that takes JSON schema and generates Java POJOs annotated with GSON annotations. I took this stuff and created a sample module in the bus repo and see how it works. It works very nicely and I think we can use this very easily in our stuff.

First, here is a site that discusses JSON schema if you aren't familiar (its essentially like XML Schema only for JSON):

http://json-schema.org/documentation.html

We can use this maven plugin to generate an API that our Java components can use to talk to each other. To see what the generated code looks like with any JSON schema, here's a play area where you can see it in realtime:

http://www.jsonschema2pojo.org/

Here's what my sample JSON schema looks like (you'll see it uses primitives as well as sets/lists and embedded types (see address type) and a set of embedded types (see phoneNumbers):

https://github.com/hawkular/hawkular-bus/blob/master/hawkular-bus-samples/hawkular-bus-sample-jsonschema/src/main/resources/schema/Person.jsonschema

Here is where my test code creates the message via the POJO API that was generated by the maven plugin:

https://github.com/hawkular/hawkular-bus/blob/master/hawkular-bus-samples/hawkular-bus-sample-jsonschema/src/main/java/org/hawkular/bus/sample/client/Main.java#L84

If we use this stuff, we write the JSON schema which automatically provides documentation that non-Java clients can use to know what the clients need to send/consume, or they can use the schema themselves to generate their own client code. We use the schema internally to generate Java POJOs that we then use in our Java components (like what my sample code demonstrates).

Here is what the maven plugin usage looks like - its pretty simple:

https://github.com/hawkular/hawkular-bus/blob/master/hawkular-bus-samples/hawkular-bus-sample-jsonschema/pom.xml#L48

_______________________________________________
hawkular-dev mailing list
hawkular-dev at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/hawkular-dev


More information about the hawkular-dev mailing list