You need to enable JavaScript to run this app. 如何在Apache Camel中实现跨引用JSON Schema的类路径加载校验
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache Camel中实现跨引用JSON Schema的类路径加载校验

问题背景

日常开发中需要用JSON Schema完成JSON内容校验,主Schema文件中会嵌套引用其他独立的Schema资源。
在Apache Camel路由中接入JSON Schema校验能力时,由于使用的是JSON Schema draft-07版本(组件默认适配draft-04版本),按照社区通用方案自定义了JsonSchemaLoader的实现。
目前遇到的异常是:主Schematest-schema-1可以正常从类路径加载,但被引用的test-schema-2会被组件尝试从公网拉取。由于Schema中配置的IDhttp://mycompany/test-schema2.json只是标识用的虚构地址,并非可访问的真实网络资源,程序直接抛出主机不存在的异常。核心诉求为:实现所有被引用的Schema都从类路径完成加载,避免无意义的网络请求。

现有实现内容

Spring Boot 应用配置

@SpringBootApplication
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }

  @Bean
  public JsonSchemaLoader mySchemaLoader() {
    return (camelContext, schemaStream) -> 
        JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaStream);
  }

  @Bean
  public EndpointRouteBuilder routeBuilder() {
    return new EndpointRouteBuilder() {
      @Override
      public void configure() throws Exception {
        from(file("d:/tmp/camel/"))
            .to(jsonValidator("/test/test-schema1.json").advanced().schemaLoader(mySchemaLoader())).stop();
      }
    };
  }
}

Schema文件定义

主Schema test-schema-1:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://mycompany/test-schema1.json",
    "type": "object",
    "properties": {
        "obj": {
            "$ref": "test-schema2.json"
        }
    },
    "required": [
        "target"
    ]
}

被引用的Schema test-schema-2:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://mycompany/test-schema2.json",
    "type": "object",
    "properties": {
        "value": {
            "type": "string"
        }
    }
}

报错信息

程序运行到Schema校验环节抛出如下异常:

Message History (complete message history is disabled)
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [from[file://d:/tmp/camel/]                                                    ] [         5]
...
[route1            ] [to1               ] [json-validator:///test/test-schema1.json?hash=93f27f49                        ] [         0]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------

com.networknt.schema.JsonSchemaException: java.net.UnknownHostException: mycompany
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:348) ~[json-schema-validator-1.0.43.jar:na]
    at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:76) ~[json-schema-validator-1.0.43.jar:na]
    at com.networknt.schema.RefValidator.<init>(RefValidator.java:41) ~[json-schema-validator-1.0.43.jar:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:131) ~[json-schema-validator-1.0.43.jar:na]
    ...(中间堆栈省略)
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:304) ~[json-schema-validator-1.0.43.jar:na]
    at test.TestApplication.lambda$0(TestApplication.java:22) ~[classes/:na]
    at org.apache.camel.component.jsonvalidator.JsonValidatorEndpoint.getOrCreateSchema(JsonValidatorEndpoint.java:162) ~[camel-json-validator-3.5.0.jar:3.5.0]
Caused by: java.net.UnknownHostException: mycompany
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520) ~[na:na]
    at com.networknt.schema.uri.URLFetcher.fetch(URLFetcher.java:43) ~[json-schema-validator-1.0.43.jar:na]
    ... 46 common frames omitted

异常根因是:JSON Schema解析库在处理$ref引用时,识别到引用目标的ID为网络地址格式后,默认走HTTP拉取逻辑,没有匹配类路径下的本地资源。

内容的提问来源于stack exchange,提问作者dur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 16:15:10