第一步在resouce 目录下面定义好json文件
第二步,写好controller去组装api
package com.guodi.audit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.guodi.audit.dto.AuditResultMaterial;
import com.guodi.audit.dto.AuditSuggestion;
import com.guodi.common.entity.AjaxResult;
import com.guodi.frame.system.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.IOException;
import java.util.List;
@Api(value = "API - 项目审查建议", description = "规划审查项目审查接口")
@Controller
@RequestMapping("/prjAudit")
public class ProjectAuditController extends BaseController {
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectAuditController.class);
@GetMapping("/suggestions")
@ApiOperation(value = "规划审查项目审查意见")
@ResponseBody
public AjaxResult suggestionList() {
AjaxResult<List<AuditSuggestion>> result = null;
try {
String inputJson = getFileContent("classpath:projectAudit.json");
List<AuditSuggestion> list = JSON.parseObject(inputJson, new TypeReference<List<AuditSuggestion>>() {
});
result = new AjaxResult<>(list);
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(e.getStackTrace().toString());
}
return result;
}
@GetMapping("/auditResult")
@ApiOperation(value = "规划审查项目审查结果")
@ResponseBody
public AjaxResult auditResult() {
AjaxResult<List<AuditResultMaterial>> result = null;
try {
String inputJson = getFileContent("classpath:projectAuditMaterial.json");
List<AuditResultMaterial> list = JSON.parseObject(inputJson, new TypeReference<List<AuditResultMaterial>>() {
});
result = new AjaxResult<>(list);
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(e.getStackTrace().toString());
}
return result;
}
private String getFileContent(String fileName) throws IOException {
File jsonFile = ResourceUtils.getFile(fileName);
if (jsonFile != null) {
return FileUtils.readFileToString(jsonFile);
}
return null;
}
@GetMapping("/auditStrategies")
@ApiOperation(value = "规划审查项目审查方案")
@ResponseBody
public AjaxResult auditStrategies() {
AjaxResult<List<AuditResultMaterial>> result = null;
try {
String inputJson = getFileContent("classpath:projectStrategy.json");
List<AuditResultMaterial> list = JSON.parseObject(inputJson, new TypeReference<List<AuditResultMaterial>>() {
});
result = new AjaxResult<>(list);
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(e.getStackTrace().toString());
}
return result;
}
}