import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.Cleanup; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.IOException; import java.io.InputStream; public class JsonUtils { /** * 读取json配置文件 * * @param jsonPath json文件路径 * @return JSONObject格式数据 */ public static JSONObject getJSONObject (String jsonPath) { Resource resource = new ClassPathResource(jsonPath); JSONObject jsonObject = new JSONObject(); try { if (resource.exists()) { // 自动关闭流 @Cleanup InputStream inputStream = resource.getInputStream(); Object object = JSON.parseObject(inputStream, JSONObject.class); if (object instanceof JSONObject) { jsonObject = (JSONObject) object; } } } catch (IOException e) { return jsonObject; } return jsonObject; } }