Open fayeah opened 4 years ago
Springboot中,有时候会出现连接外部db但是又极慢的情况,那么这个时候可能就需要自己弄一个json文件,方便本地调试:
steps:
创建一个含有所需数据的json文件(注意这个在root folder最方便);
加入Gson dependency, 在dependencies里面:
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
实现:
JSONParser jsonParser = new JSONParser(); List<Model> items = null; try (FileReader reader = new FileReader("file.json")) { Object obj = jsonParser.parse(reader); JSONArray list = (JSONArray)obj; Gson gson = new Gson(); Type type = new TypeToken<List<Model>>(){}.getType(); items = gson.fromJson(list.toJSONString(), listType); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } catch(ParseException e) { e.printStackTrace(); } return items;
Note:
Springboot中,有时候会出现连接外部db但是又极慢的情况,那么这个时候可能就需要自己弄一个json文件,方便本地调试:
steps:
创建一个含有所需数据的json文件(注意这个在root folder最方便);
加入Gson dependency, 在dependencies里面:
实现:
Note: