Java 在 JSON 模式中引用本地相关文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22796063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Referencing a local relative file in a JSON Schema?
提问by Sled
I am building a JSON for a REST web service. The schema however has grown to over 1300 lines and I want to split it up across multiple files. I am unit testing my schema against a folder of JSON sample responses using json-schema-validatorversion 2.1.8.
我正在为 REST Web 服务构建 JSON。然而,模式已经增长到 1300 多行,我想将它拆分到多个文件中。我正在使用json-schema-validator版本 2.1.8针对 JSON 示例响应文件夹对我的架构进行单元测试。
I know you have import types defined in your current file like { "$ref": "#/definitions/MyBool" }
, however if I wanted to move the definition of MyBool
to another file, like Common.schema.json
, how would I reference it then?
我知道您在当前文件中定义了导入类型,例如{ "$ref": "#/definitions/MyBool" }
,但是如果我想将 的定义移动MyBool
到另一个文件中,例如Common.schema.json
,那么我将如何引用它?
I tried { "$ref": "Common.schema.json/definitions/MyBoolean" }
, { "$ref": "./Common.schema.json/defintion/MyBoolean" }
, and { "$ref": "file://./Common.schema.json/definitions/MyBoolean" }
but none of them work.
我试过{ "$ref": "Common.schema.json/definitions/MyBoolean" }
, { "$ref": "./Common.schema.json/defintion/MyBoolean" }
,{ "$ref": "file://./Common.schema.json/definitions/MyBoolean" }
但它们都不起作用。
The answer on "Does JSON Schema validation in common-js utils support references?" seems like it should work but I can't seem to get the syntax right.
“ Common-js utils 中的 JSON Schema 验证是否支持引用?”的答案似乎应该可行,但我似乎无法正确理解语法。
I'm loading the schema with:
我正在加载架构:
JsonNode mySchema = JsonLoader.fromReader( new InputStreamReader( JsonSchemaTest.class.getResourceAsStream( "/json/schema/MySchema.schema.json" ) ) );
and then validating it with:
然后用以下方法验证它:
JsonSchemaFactory.byDefault().getValidator().validate( schema, new InputStreamReader( getClass().getResourceAsStream( "/json/sample/MyJsonSample.json" ) ) ).isSuccess();
FWIW MyBool
looks like:
FWIWMyBool
看起来像:
"MyBool": {
"type": "object",
"properties": {
"value" :{
"type": "string",
"enum": [ "true", "false", "file not found" ]
}
},
"required": ["value"],
"additionalProperties": false
}
When I don't try to extract MyBool
, the schema validates all my JSON samples correctly.
当我不尝试提取时MyBool
,模式会正确验证我的所有 JSON 样本。
The exception I receive is:
我收到的例外是:
com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: unable to dereference URI "file:/Common.schema.json/definitions/MyBool#"
level: "fatal"
uri: "file:/Common.schema.json/definitions/MyBool#"
exceptionMessage: "\Common.schema.json\definitions\MyBool (The system cannot find the path specified)"
at com.github.fge.jsonschema.core.load.URIManager.getContent(URIManager.java:108)
at com.github.fge.jsonschema.core.load.SchemaLoader.load(SchemaLoader.java:108)
at com.github.fge.jsonschema.core.load.SchemaLoader.load(SchemaLoader.java:103)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at com.github.fge.jsonschema.core.load.SchemaLoader.get(SchemaLoader.java:165)
at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:113)
at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:50)
at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:76)
at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:40)
at com.github.fge.jsonschema.core.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:188)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.core.processing.CachingProcessor.load(CachingProcessor.java:127)
at com.github.fge.jsonschema.core.processing.CachingProcessor.load(CachingProcessor.java:119)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:108)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:106)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:55)
at com.github.fge.jsonschema.core.processing.ProcessorMap$Mapper.process(ProcessorMap.java:165)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.core.processing.CachingProcessor.load(CachingProcessor.java:127)
at com.github.fge.jsonschema.core.processing.CachingProcessor.load(CachingProcessor.java:119)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:108)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:83)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:179)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:121)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processArray(ValidationProcessor.java:149)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:119)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:179)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:121)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
at com.github.fge.jsonschema.keyword.validator.draftv4.OneOfValidator.validate(OneOfValidator.java:67)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:100)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:103)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:123)
at com.initech.ws.json.BaseJsonSchemaTest.jsonFileShouldValidate(BaseJsonSchemaTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.accessfinal JsonSchema schema
= factory.getJsonSchema("resource:/json/schema/MySchema.schema.json");
0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access{
"$ref": "MyBoolean.json#/pointer/into/file"
}
0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access{
"$ref": "subschemas/MyBoolean.json#/pointer/into/file"
}
0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
采纳答案by fge
What you need there is being able to reference your files using an absolute URI.
您需要的是能够使用绝对 URI 引用您的文件。
Supposing you are using the stable version (ie 2.0.2), load your schema using this method.
假设您使用的是稳定版本(即 2.0.2),请使用此方法加载您的架构。
Given where your file is located, use:
鉴于您的文件所在的位置,请使用:
"items": {
"type": "object",
"$ref": "file:src/xyz/abc/lmn/DeviceRecord.json"
}
This means URI resolution in this schema will be made relatively to this (loading) URI; so, refering to your MyBoolean.json
, you will do:
这意味着此模式中的 URI 解析将与此(加载)URI 相关;因此,参考您的MyBoolean.json
,您将执行以下操作:
If it is located at, for instance, /json/schema/subschemas
then you will write:
如果它位于,例如,/json/schema/subschemas
那么你会写:
Parents also work etc.
父母也工作等等。
Note that as noted in the README, 2.1.x is a development version! I am currently reworking the API...
请注意,如 README 中所述,2.1.x 是开发版本!我目前正在修改 API...
回答by apurbojha
The following solution worked for me. The path is relative to location where I run mvn goals.
以下解决方案对我有用。该路径与我运行 mvn 目标的位置有关。
##代码##Here file:
is the path from where you are executing the maven goal.
这file:
是您执行 maven 目标的路径。