读取resource下文件

ArrayList<PatrolOper> patrolOpers = new ArrayList<>();

String jsonData = null;
File jsonFile = null;
try {
jsonFile = ResourceUtils.getFile("classpath:jsonRequest.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
jsonData = FileUtils.readFileToString(jsonFile,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}


Gson gson = new Gson();
PatrolTaskOperRequest bean = gson.fromJson(jsonData, PatrolTaskOperRequest.class);

主要在于,加载了好几次,都发现class下没json文件,后来发现,maven 对resource有过滤。
<!--配置Maven 对resource文件 过滤 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.json</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
posted @ 2018-06-04 17:28  wlhebut  阅读(1997)  评论(0编辑  收藏  举报