JSON-Schema generation in Jersey

So in my previous post I talked about a proposal to allow the use of JSON-Schema in a WADL, this post looks at how to get this working with a recently build of Jersey. You are going to have to download / reference 1.16SNAPSHOT until 1.16 is released.

If you are using Maven this should be quite straight forward to update your dependencies assuming you already have jersey and jersey-json. You are just going to need to add a dependency on the ‘jersey-wadl-json-schema’ artefact from the ‘com.sun.jersey.contribs’ group to get the new feature.

If you are outside of Maven the easiest thing to do would be to download the latest jersey-archive and then the jersey-wadl-json-schema jar. How you deploy these is tool specific, but if you are using WLS then here are some specific notes on how to upgrade the version of Jersey.

Once you have this working, you need to create a WadlGeneratorConfig class in order to enable this new grammar generation:

package jersey;

import com.sun.jersey.api.wadl.config.WadlGeneratorConfig;
import com.sun.jersey.api.wadl.config.WadlGeneratorDescription;
import com.sun.jersey.wadl.generators.json.WadlGeneratorJSONGrammarGenerator;

import java.util.List;

public class JsonGeneratorConfig extends WadlGeneratorConfig {

    public List

               configure() {
        return generator(WadlGeneratorJSONGrammarGenerator.class).descriptions();

This can then be registered in a variety of ways, here is an example using a servlet init param. Note also that to make this example simple that we are using the Jersey POJO mapping; but whilst writing this blog I noticed that this setting affected the format of the JSON version of the WADL in case you try this.

<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<web-app xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd'

So I put together a really simple echo service, just to check this is all working:

package jersey;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

public class EchoResource {

    public EchoBean echo() {
        EchoBean bean = new EchoBean();
        return bean;

    public EchoBean echo(EchoBean echo) {
        return echo;



package jersey;

public class EchoBean {
    public EchoBean() {

    private String message;

    public void setMessage(String message) {
        this.message = message;

    public String getMessage() {
        return message;


This very simple example results in the following WADL with the JSON-Schema elements referenced:

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:application xmlns:ns0='http://wadl.dev.java.net/2009/02'>
   <ns0:doc xmlns:ns1='http://jersey.java.net/' ns1:generatedBy='Jersey: 1.16-SNAPSHOT 11/19/2012 12:59 AM'/>
   <ns0:resources base='http://localhost:7103/Jersey/resources/'>
      <ns0:resource path='/echo'>
         <ns0:method id='echo' name='GET'>
               <ns0:representation mediaType='application/json' xmlns:ns2='http://wadl.dev.java.net/2009/02/json-schema' ns2:describedby='application.wadl/echoBean'/>
         <ns0:method id='echo' name='POST'>
               <ns0:representation mediaType='application/json' xmlns:ns3='http://wadl.dev.java.net/2009/02/json-schema' ns3:describedby='application.wadl/echoBean'/>
               <ns0:representation mediaType='application/json' xmlns:ns4='http://wadl.dev.java.net/2009/02/json-schema' ns4:describedby='application.wadl/echoBean'/>

The URI application.wadl/echoBean contains this simple JSON-Schema definition:

    'type' : 'object',
    'properties' : {
        'message' : {
            'type' : 'string'
    'name' : 'echoBean'

Now there are a number of limitations in the current design, not least that the generated schema doesn’t take into account any notation settings. But I thought this would be enough to provoke feedback on whether this feature would be useful generally. There appears to be a growing interest in JSON-Schema both around the net and internally to Oracle, so it would be interesting to see whether this description becomes more common.

Reference: JSON-Schema generation in Jersey from our JCG partner Gerard Davison at the Gerard Davison’s blog blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

9 − four =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books