【问题标题】:Reading or loading JSON file from another module从另一个模块读取或加载 JSON 文件
【发布时间】:2016-10-06 04:05:05
【问题描述】:

我想从另一个模块的/resources 目录读取/加载JSON 文件到当前模块并使用jackson 映射JSON

当前模块名称为“transform”,而我需要的资源文件位于另一个名为“schema”的模块中。我在“转换”模块的pom.xml 文件中添加了“模式”作为maven dependency

我需要在“schema”模块中读取/加载一个名为“example.json”的文件。

但我不断收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input.

到目前为止我所拥有的:

Map<String, String> map = objectMapper.readValue(
                getClass().getClassLoader().getResourceAsStream("/schema/resources/example.json"),
                                                new TypeReference<Map<String, String>>(){});

假设 example.json 的完整路径是“/schema/resources/example.json”。

【问题讨论】:

标签: java json maven resources


【解决方案1】:
Map<String, String> map = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResourceAsStream("/schema/resources/example.json"),
                                            new TypeReference<Map<String, String>>(){});

【讨论】:

  • 谢谢!我尝试了一些非常相似的东西,它奏效了。这是我所做的:objectMapper.readValue(MyClassInDifferentModule.class.getResourceAsStream("/schema/resources/example.json"), new TypeReference&lt;HashMap&lt;String, Object&gt;&gt;(){});
猜你喜欢
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 2019-02-21
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
相关资源
最近更新 更多