1

我正在尝试使http://json-schema.org/draft-04/schema#在本地存储库中。但它无法在任何路径中找到引用的 JSON,无论是根目录、资源还是当前目录,或者直接在 jar 中。

由于这些是运行时异常,因此资源中的 json-schema.org/draft-04/schema.json 文件以及 jar 似乎不在打包的构建中。

有没有办法,我可以在 build.gradle 中的 JavaCompile 中定义来选择 json 文件

下面是代码片段

架构.json:

   {
    "$ref": "file://json-schema-draft-04-schema.json",
    "type": "object",
    "title": "Hyperledger Fabric Contract Definition JSON Schema",
    "required": [
        "info",
        "contracts"
    ],
    "properties": {
        "info": {
            "$ref": "#/definitions/info"
        },
        "contracts": {
            "type": "object",
            "patternProperties": {
                "^.*$": {
                    "$ref": "#/definitions/contract"
                }
            }
        },
        "components": {
            "$ref": "#/definitions/components"
        }
    },
    "definitions": {
        "info": {
            "type": "object",
            "description": "General information about the API.",
            "required": [
                "version",
                "title"
            ],
            "properties": {
                "title": {
                    "type": "string",
                    "description": "A unique and precise title of the API."
                },
                "version": {
                    "type": "string",
                    "description": "A semantic version number of the API."
                },
                "description": {
                    "type": "string",
                    "description": "A longer description of the API. Should be different from the title.  GitHub Flavored Markdown is allowed."
                },
                "termsOfService": {
                    "type": "string",
                    "description": "The terms of service for the API."
                },
                "contact": {
                    "$ref": "#/definitions/contact"
                },
                "license": {
                    "$ref": "#/definitions/license"
                }
            }
        },

参考json json-schema-draft-04-schema.json:

   {
    "id": "http://json-schema.org/draft-04/schema#",
    "description": "Core schema meta-schema",
    "definitions": {
        "schemaArray": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#" }
        },
        "positiveInteger": {
            "type": "integer",
            "minimum": 0
        },
        "positiveIntegerDefault0": {
            "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
        },
        "simpleTypes": {
            "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
        },
        "stringArray": {
            "type": "array",
            "items": { "type": "string" },
            "minItems": 1,
            "uniqueItems": true
        }
    },
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "$schema": {

错误报价

   0|Exception in thread "main" java.io.UncheckedIOException: java.io.FileNotFoundException: /json-schema-draft-04-schema.json (No such file or directory)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:20)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.JsonPointerEvaluator.executeWith(JsonPointerEvaluator.java:78)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.JsonPointerEvaluator.lambda$forURL$1(JsonPointerEvaluator.java:121)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.JsonPointerEvaluator.query(JsonPointerEvaluator.java:151)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.ReferenceLookup.lookup(ReferenceLookup.java:173)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.ReferenceSchemaExtractor.extract(SchemaExtractor.java:193)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:383)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434)
dev-peer0.org1.example.com-papercontract-0|     at org.hyperledger.fabric.contract.metadata.MetadataBuilder.validate(MetadataBuilder.java:87)
dev-peer0.org1.example.com-papercontract-0|     at org.hyperledger.fabric.contract.metadata.MetadataBuilder.initialize(MetadataBuilder.java:115)
dev-peer0.org1.example.com-papercontract-0|     at org.hyperledger.fabric.contract.ContractRouter.main(ContractRouter.java:134)
dev-peer0.org1.example.com-papercontract-0|Caused by: java.io.FileNotFoundException: /json-schema-draft-04-schema.json (No such file or directory)
dev-peer0.org1.example.com-papercontract-0|     at java.io.FileInputStream.open0(Native Method)
dev-peer0.org1.example.com-papercontract-0|     at java.io.FileInputStream.open(FileInputStream.java:195)
dev-peer0.org1.example.com-papercontract-0|     at java.io.FileInputStream.<init>(FileInputStream.java:138)
dev-peer0.org1.example.com-papercontract-0|     at java.io.FileInputStream.<init>(FileInputStream.java:93)
dev-peer0.org1.example.com-papercontract-0|     at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
dev-peer0.org1.example.com-papercontract-0|     at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
dev-peer0.org1.example.com-papercontract-0|     at java.net.URLConnection.getContent(URLConnection.java:739)
dev-peer0.org1.example.com-papercontract-0|     at java.net.URL.getContent(URL.java:1059)
dev-peer0.org1.example.com-papercontract-0|     at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:18)
dev-peer0.org1.example.com-papercontract-0|     ... 13 more

构建.gradle

   plugins {

    id 'java'
    //id 'application'
    id 'com.github.johnrengelman.shadow' version '2.0.3'

}

version '0.0.1'

sourceCompatibility = 1.8

repositories {

    // flatDir { dirs 'libs' }
    mavenLocal()
    maven {
        url 'http://repo1.maven.org/maven2'
    }
    maven {
        url 'https://jitpack.io'
    }
    maven {
        url "https://nexus.hyperledger.org/content/repositories/snapshots/"
    }


}

dependencies {
    //compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim',version: '1.4.3'
    compile fileTree(dir: 'libs', include: '*.jar') // local fabric-chaincode-shim which has json
    // schema locally store in the jar. Also, there is one in resources folder.
    compile group: 'org.hyperledger.fabric-sdk-java', name: 'fabric-sdk-java', version: '1.4.4'
    compile 'org.reflections:reflections:0.9.11'
    implementation 'com.github.everit-org.json-schema:org.everit.json.schema:1.11.1'
    compile 'io.swagger.core.v3:swagger-annotations:2.0.0'
    compile group: 'org.json', name: 'json', version: '20180813'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    testImplementation 'org.assertj:assertj-core:3.11.1'
    testImplementation 'org.mockito:mockito-core:2.+'
}

shadowJar {
    baseName = 'chaincode'
    version = null
    classifier = null

    manifest {
        attributes 'Main-Class': 'org.hyperledger.fabric.contract.ContractRouter'
    }
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}


tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-parameters"
}

这两个文件都在一个可执行的 jar 中。我也尝试将参考 .json 文件放在项目的根目录中,但没有运气。也尝试使用 classpath://。没运气。

4

1 回答 1

1

这里有两个可能的问题。

首先,您假设您使用的库支持该file://协议。是否专门记录了您可以在正在使用的库中以这种方式引用本地文件?JSON Schema 本身不对协议支持做任何假设,也不知道自己是文件夹结构中的文件。

其次,您不能$ref在同一对象中与其他关键字一起使用。即使您的参考确实解决了,它也会使架构中的所有其他关键字被忽略。您需要使用allOf.

于 2019-08-27T08:40:02.517 回答