Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2014, Francis Galiegue (
  * This software is dual-licensed under:
  * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
  *   later version;
  * - the Apache Software License (ASL) version 2.0.
 * The text of both licenses is available under the src/resources/ directory of
 * this project (under the names LGPL-3.0.txt and ASL-2.0.txt respectively).
 * Direct link to the sources:
 * - LGPL 3.0:
 * - ASL 2.0:
package com.github.fge.jsonschema.examples;
Sixth example: URI redirection

link to source code

In this example, the same schema file is used as in Example1. This time, though, it is assumed that the base URI used for addressing this schema is But instead of trying to fetch it from the web directly, we want to use the local copy, which is located under URI resource:/org/eel/kitchen/jsonschema/examples/fstab.json#.

The solution here is to build a custom com.github.fge.jsonschema.core.load.uri.URITranslatorConfiguration, which allows to customize URI handling; in this case, a schema redirection using the com.github.fge.jsonschema.core.load.uri.URITranslatorConfigurationBuilder.addSchemaRedirect(java.lang.String,java.lang.String). We then inject this into a custom com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration.

The effect is that if you required a schema via URI, it will silently transform this URI into resource:/org/eel/kitchen/jsonschema/examples/fstab.json# internally.

Note that URIs must be absolute JSON references (see com.github.fge.jsonschema.core.ref.JsonRef).

public final class Example6
    private static final String FROM = "";
    private static final String TO
        = "resource:/com/github/fge/jsonschema/examples/fstab.json#";
    public static void main(final String... args)
        throws IOExceptionProcessingException
        final JsonNode good = Utils.loadResource("/fstab-good.json");
        final JsonNode bad = Utils.loadResource("/fstab-bad.json");
        final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
        final URITranslatorConfiguration translatorCfg
            = URITranslatorConfiguration.newBuilder()
        final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
        final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
        final JsonSchema schema = factory.getJsonSchema();
        ProcessingReport report;
        report = schema.validate(good);
        report = schema.validate(bad);
        report = schema.validate(bad2);
New to GrepCode? Check out our FAQ X