9

I am using Jersey 1.2 (still using JDK 1.5) and have developed a REST Booking Web Resource and an associated Booking POJO.

I have used Bean Validation to restrict the size/type of the fields e.g.

@NotNull
@Size(message="invalid size",min=3,max=20)
@Pattern(message="invalid pattern",regexp = "^[A-Za-z]*")
@JsonProperty(value = "forename")
private String forename;

and have used the Jackson ObjectMapper.generateJsonSchema class to generate the JSON schema however this ignores all the validation annotations so I just get:

"forename" : {
  "type" : "string"
}

Is there any way to include the restriction information as part of the generated schema?

Many thanks

MandyW
  • 1,117
  • 3
  • 14
  • 23

3 Answers3

7

Looks like jackson-module-jsonSchema 2.5.0+ now supports some of the annotations now:

ValidationSchemaFactoryWrapper personVisitor = new    
ValidationSchemaFactoryWrapper();
ObjectMapper mapper = new ObjectMapper();
mapper.acceptJsonFormatVisitor(Person.class, personVisitor);
JsonSchema personSchema = personVisitor.finalSchema();
indybee
  • 1,507
  • 13
  • 17
6

There is no way to do that out-of-the-box, but this extension module:

https://github.com/FasterXML/jackson-module-jsonSchema

should let you modify Schema Generator in a way to add whatever extra information you want. Caveat: this is a new project and probably requires you to use 2.2.0-SNAPSHOT version of core Jackson components.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thanks very much for replying, I'll take a look and try it out. Have up voted the answer and will mark as correct once I finish trying it out! – MandyW Jan 04 '13 at 11:59
1

Just to say this is how I fixed this issue.. I used the very nice jsonschema2pojo plugin with the flag - includeJsr303Annotations

<build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.11</version>
                <configuration>
                    <includes>
                        <include>*.json</include>
                    </includes>
                    <useJodaDates>true</useJodaDates>
                    <sourceDirectory>${basedir}/src/main/resources/webapi/jsonschema</sourceDirectory>
                    <targetPackage>com.ba.schema.json.jsonschemav02</targetPackage>
                    <includeJsr303Annotations>true</includeJsr303Annotations>
                    <includeHashcodeAndEquals>false</includeHashcodeAndEquals>
                    <includeToString>false</includeToString>
                    <outputDirectory>${project.build.directory}/generated-sources/xjc/</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

I haven't tried jackson-module-jsonSchema 2.5.0+ yet as the above does what I need. Thanks to all for your help.

MandyW
  • 1,117
  • 3
  • 14
  • 23