springBoot 獲取靜態json文件信息

/**
 * @Description 讀取靜態json文件信息
 * @Author lxy
 * @Date 2019/5/21 17:37
 */
public static  <T> List<T> readJsonFromClassPath(String path, Type type) throws IOException {

    ClassPathResource resource = new ClassPathResource(path);
    if (resource.exists()) {
        return JSON.parseObject(
                resource.getInputStream(),
                StandardCharsets.UTF_8,
                type,
                // 自動關閉流
                Feature.AutoCloseSource,
                // 允許註釋
                Feature.AllowComment,
                // 允許單引號
                Feature.AllowSingleQuotes,
                // 使用 Big decimal
                Feature.UseBigDecimal);
    }
    return null;
}

 

 

@GetMapping("/log")
@ExcludeRequestParam
public Response getUpdateLog() {
    try {
        return HttpUtil.getSuccessResponse(VersionUtil.readJsonFromClassPath("json/version.json", List.class));
    } catch (Exception e) {
        log.error("Parsing json error,the path is 'json/version.json', class is  'UpdateLogResponse.class' , message is {}", e.getMessage());
    }

    return HttpUtil.getFailResponse(ResponseEnum.NOT_FIND_DATA);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章