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

原文地址:https://www.cnblogs.com/javalinux/p/14809206.html