springboot 讀取靜態json文件


package com.yonyougov.fbpm.modeler.controller;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.element.Name;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Objects;


/**
* @author liwenbo
* @Date 2019/12/11 19:15
* @Description
*/
@RestController
@RequestMapping("/api/proc/config/")
public class ConfigController {

@Value("classpath:static/config/admin-config.json")
private Resource adminConfig;
@Value("classpath:static/config/components.json")
private Resource components;

@GetMapping("readFile/{fileName}")
public String test(@PathVariable String fileName) {
Resource resource = null;
if (Objects.equals(fileName, "components")) {
resource = components;
}
if (Objects.equals(fileName, "admin-config")) {
resource = adminConfig;
}
if (StringUtils.isEmpty(fileName) || Objects.isNull(resource)) {
return "文件不存在";
}
try {
String areaData = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));
return areaData;
} catch (IOException e) {
e.printStackTrace();
}
return "文件不存在";
}

}
————————————————
版權聲明:本文為CSDN博主「蝸牛2219」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_36614280/article/details/103504633


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM