You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rest Assured 5.4.0验证JSON Schema时遇无JSON文本读取错误

解决Rest Assured 5.4.0 JSON Schema验证时的"no JSON Text to read from input"异常

针对你遇到的这个JSON Schema验证异常,以下是几个针对性的排查和解决方向:

1. 确认Schema文件的路径与存在性

你使用matchesJsonSchemaInClasspath方法加载Schema,需确保:

  • 文件assortment/schema/postResponseSchema.json确实存在于项目的classpath目录中(比如Maven项目要放在src/test/resources/assortment/schema/下)
  • 路径拼写完全正确,注意大小写(部分操作系统区分路径大小写)

可以先通过代码验证文件是否能被读取:

InputStream schemaStream = ClassLoader.getSystemResourceAsStream("assortment/schema/postResponseSchema.json");
if (schemaStream == null) {
    throw new RuntimeException("Schema文件未找到,请检查路径");
}

2. 排查Schema文件的内容格式问题

即便在线验证Schema有效,也可能存在本地文件的编码或隐形字符问题:

  • 检查文件是否带有UTF-8 BOM头(Windows下保存的文件容易出现),可以用Notepad++等工具将文件转为UTF-8无BOM格式重新保存
  • 手动读取Schema内容并输出,确认没有空行、不可见字符或格式损坏:
String schemaContent = new String(
    ClassLoader.getSystemResourceAsStream("assortment/schema/postResponseSchema.json").readAllBytes(),
    StandardCharsets.UTF_8
);
System.out.println("Schema内容: " + schemaContent);
// 改用直接传入内容的方式验证
.body(JsonSchemaValidator.matchesJsonSchema(schemaContent))

3. 检查接口响应体是否有效

异常提示"无JSON文本可读",也可能是接口返回的响应体为空或非JSON格式:

  • 查看.log().all()输出的响应内容,确认接口是否返回了预期的JSON数据,而非空内容、HTML错误页面(如500错误)或其他格式
  • 先验证接口响应的状态码是否为2xx,再执行Schema验证:
.then().log().all()
.statusCode(200)
.assertThat()
.body(JsonSchemaValidator.matchesJsonSchemaInClasspath("assortment/schema/postResponseSchema.json"))

4. 简化你的Schema(可选)

你的Schema中stocks数组的items字段使用了数组形式,但两个对象结构完全一致,这在JSON Schema中是冗余的。可以简化为:

{"$schema": "http://json-schema.org/draft-04/schema#","type": "object","properties": {"date": {"type": "string"},"stocks": {"type": "array","items": {"type": "object","properties": {"nnt": {"type": "string"},"sku": {"type": "string"},"qnt": {"type": "integer"},"supInn": {"type": "string"},"supId": {"type": "string"},"supDate": {"type": "string"},"nds": {"type": "integer"},"gnvls": {"type": "boolean"},"dp": {"type": "boolean"},"man": {"type": "string"},"exp": {"type": "string"},"prcMan": {"type": "number"},"prcOpt": {"type": "number"},"prcOptNds": {"type": "number"},"prcRet": {"type": "integer"},"prcReestr": {"type": "number"},"prcGnvlsMax": {"type": "number"},"sumOpt": {"type": "number"},"sumOptNds": {"type": "number"},"mrGnvls": {"type": "number"},"tag": {"type": "integer"},"nomenclatureId": {"type": "integer"}},"required": ["nnt","sku","qnt","supInn","supId","supDate","nds","gnvls","dp","man","exp","prcMan","prcOpt","prcOptNds","prcRet","prcReestr","prcGnvlsMax","sumOpt","sumOptNds","mrGnvls","tag","nomenclatureId"]}}},"required": ["date","stocks"]}

冗余的数组形式可能导致解析器误判,简化后能减少潜在的验证问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 16:08:13