SpringBoot通过配置读取json文件

时间:2025-03-18 15:12:57
@Configuration public class JsonFileConfigUtil { @Bean public List<LeafOrgInfoDTO> getLeafOrgInfos() throws IOException { String path = "/"; InputStream inputStream = getClass().getResourceAsStream(path); if(inputStream==null){ throw new RuntimeException("读取限流组织json文件失败!!!"); }else{ JSONObject json = JSON.parseObject(inputStream,JSONObject.class); JSONArray array = json.getJSONArray("list"); List<LeafOrgInfoDTO> list = array.toJavaList(LeafOrgInfoDTO.class); return list; } } @Bean public LeafOrgInfoList getLeafOrgInfoList() throws IOException { Resource resource = new ClassPathResource("/"); InputStream inputStream = resource.getInputStream(); return new ObjectMapper().readValue(inputStream, LeafOrgInfoList.class); } }