Java 尝试使用其模式验证json时发生异常

Java 尝试使用其模式验证json时发生异常,java,json,jsonschema,Java,Json,Jsonschema,[ 嗨,蒂哈默 我尝试了你的代码,但是我得到了异常..我尝试了使用所有的jar,但是它给出了NoclassFoundException,我尝试了导入所有相关的jar 下面是我的代码 import java.io.IOException; import java.util.Iterator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonNode; im

[

嗨,蒂哈默

我尝试了你的代码,但是我得到了异常..我尝试了使用所有的jar,但是它给出了NoclassFoundException,我尝试了导入所有相关的jar

下面是我的代码

import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jackson.JsonLoader;

public class JsonValidation {
    public static void main(String[] args) {

        JsonValidation jv = new JsonValidation();
        String jsonData = "{\"dispatcherMode\":\"standard\",\"noOfdispatcher\":\"3\",\"dispatcherInfo\":[{\"dispatcher\":\"Dispatcher 0 = 156.95.53.243:6108<--current\"},{\"dispatcher\":\"Dispatcher 1 = 156.95.53.220:6108\"},{\"dispatcher\":\"Dispatcher 2 = 172.26.41.113:6108\"}],\"noOfAuthServer\":\"3\",\"connected\":\"3\",\"authorizationInfo\":[{\"authServer\":\"authserver 0 = 172.26.41.114:6115(connected)<--current\",\"requests\":\"1503\",\"failures\":\"1\",\"queued\":\"0\",\"delay\":\"10225\"},{\"authServer\":\"authserver 1 = 156.95.53.220:6115(connected)\",\"requests\":\"10745\",\"failures\":\"0\",\"queued\":\"0\",\"delay\":\"5762\"},{\"authServer\":\"authserver 2 = 172.26.41.113:6115(connected)\",\"requests\":\"12545\",\"failures\":\"1\",\"queued\":\"0\",\"delay\":\"9756\"}],\"noOfCache\":\"2\",\"cacheInfo\":[{\"cacheNumber\":\"cache 0\",\"request\":\"0\",\"hits\":\"0\",\"entries\":\"0\",\"size\":\"10000\",\"ttl\":\"600sec\"},{\"cacheNumber\":\"cache 1\",\"request\":\"1\",\"hits\":\"1\",\"entries\":\"1\",\"size\":\"12000\",\"ttl\":\"300sec\"}]}";
        String jsonSchema = "{\"type\": \"object\",\"properties\": {\"dispatcherMode\": {\"type\": \"string\"},\"noOfdispatcher\": {\"type\": \"string\"},\"dispatcherInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}}]},\"noOfAuthServer\": {\"type\": \"string\"},\"connected\": {\"type\": \"string\"},\"authorizationInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}}]},\"noOfCache\": {\"type\": \"string\"},\"cacheInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"cacheNumber\": {\"type\": \"string\"},\"request\": {\"type\": \"string\"},\"hits\": {\"type\": \"string\"},\"entries\": {\"type\": \"string\"},\"size\": {\"type\": \"string\"},\"ttl\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"cacheNumber\": {\"type\": \"string\"},\"request\": {\"type\": \"string\"},\"hits\": {\"type\": \"string\"},\"entries\": {\"type\": \"string\"},\"size\": {\"type\": \"string\"},\"ttl\": {\"type\": \"string\"}}}]}},\"required\": [\"dispatcherMode\",\"noOfdispatcher\",\"dispatcherInfo\",\"noOfAuthServer\",\"connected\",\"authorizationInfo\",\"noOfCache\",\"cacheInfo\"]}";
        jv.validate(jsonData, jsonSchema);
    }

    public boolean validate(String  jsonData, String jsonSchema)
    {
        ProcessingReport report = null;
        boolean result = false;
        try{
            JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
            JsonNode dataNode = JsonLoader.fromString(jsonData);

            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            JsonSchema schema = factory.getJsonSchema(schemaNode);
            report = schema.validate(dataNode);
        }
        catch (JsonParseException jpex) {
            jpex.printStackTrace();
        } catch (ProcessingException pex) {  
            pex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*catch(Exception ex){
            ex.printStackTrace();
            System.err.println(ex.getMessage());
            return false;
        }*/

        if (report != null) {
            Iterator<ProcessingMessage> iter = report.iterator();
            while (iter.hasNext()) {
                ProcessingMessage pm = iter.next();
                System.out.println("Processing Message: "+pm.getMessage());
            }
            result = report.isSuccess();
        }
        System.out.println(" Result=" +result);
        return result;
    }
}
import java.io.IOException;
导入java.util.Iterator;
导入com.fasterxml.jackson.core.JsonParseException;
导入com.fasterxml.jackson.databind.JsonNode;
导入com.github.fge.jsonschema.core.exceptions.ProcessingException;
导入com.github.fge.jsonschema.main.jsonschema;
导入com.github.fge.jsonschema.main.JsonSchemaFactory;
导入com.github.fge.jsonschema.core.report.ProcessingMessage;
导入com.github.fge.jsonschema.core.report.ProcessingReport;
导入com.github.fge.jackson.JsonLoader;
公共类JsonValidation{
公共静态void main(字符串[]args){
JsonValidation jv=新的JsonValidation();

字符串jsonData=“{\'dispatcherMode\”:\“standard\”、\“noOfdispatcher\”:\“3\”、\“dispatcherInfo\”:[{\'dispatcher\”:\”Dispatcher 0=156.95.53.243:6108 json模式验证器需要一些库,其中一个是Joda Time。似乎您没有将其添加到项目的类路径中。我一直在添加jar,但它总是不断给出其他异常……代码中是否有错误,或者这只是关于jar丢失的问题?这只是关于jar丢失的问题。i建议您将项目转换为maven项目,添加依赖项(JAR)会容易得多。我记得上次使用JSon模式验证程序时,它有很多依赖项(JAR)您最好将其转换为maven项目。@JorgeCampos谢谢。只有jar文件丢失了,我尝试添加了所有需要的jar,但成功了。json模式验证程序需要一些库,其中一个是Joda Time。您似乎没有将其添加到项目的类路径中。我一直在添加jar,但是它总是不断地给出其他异常……代码中是否有错误,或者这只是关于jar丢失的问题?这只是关于jar丢失的问题。我建议您将项目转换为maven项目,添加依赖项(jar)会容易得多。我记得上次使用JSon模式验证程序时,它有很多依赖项(jar)您最好将其转换为maven项目。@JorgeCampos谢谢。.只有jar文件丢失了,我尝试添加了所有需要的jar,但成功了。
Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/format/DateTimeFormatterBuilder
    at com.github.fge.jsonschema.format.common.DateTimeAttribute.<clinit>(DateTimeAttribute.java:50)
    at com.github.fge.jsonschema.library.format.CommonFormatAttributesDictionary.<clinit>(CommonFormatAttributesDictionary.java:52)
    at com.github.fge.jsonschema.library.format.DraftV3FormatAttributesDictionary.<clinit>(DraftV3FormatAttributesDictionary.java:47)
    at com.github.fge.jsonschema.library.DraftV3Library.<clinit>(DraftV3Library.java:32)
    at com.github.fge.jsonschema.cfg.ValidationConfigurationBuilder.<clinit>(ValidationConfigurationBuilder.java:63)
    at com.github.fge.jsonschema.cfg.ValidationConfiguration.newBuilder(ValidationConfiguration.java:92)
    at com.github.fge.jsonschema.cfg.ValidationConfiguration.byDefault(ValidationConfiguration.java:102)
    at com.github.fge.jsonschema.main.JsonSchemaFactoryBuilder.<init>(JsonSchemaFactoryBuilder.java:68)
    at com.github.fge.jsonschema.main.JsonSchemaFactory.newBuilder(JsonSchemaFactory.java:123)
    at com.github.fge.jsonschema.main.JsonSchemaFactory.byDefault(JsonSchemaFactory.java:113)
    at JsonValidation.validate(JsonValidation.java:33)
    at JsonValidation.main(JsonValidation.java:22)
Caused by: java.lang.ClassNotFoundException: org.joda.time.format.DateTimeFormatterBuilder
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 12 more