如何在控制器中只调用一次方法
How to calla method only once in a Controller
我有一个 REST API,我想调用一个从 csv 文件创建 TreeMap 的方法,我想将 TreeMap 用于每个 API calls.So 我只想调用方法来设置 TreeMap 并想将 TreeMap 用于其余的 API 调用。
我创建 TreeMap 的方法是
public void createTreeMap(){
CSVReader reader = new CSVReader(new FileReader("C:\Users\result.csv"), ',' , '"' , 1);
TreeMap <Integer,ArrayList<Long>> result=new TreeMap <Integer,ArrayList<Long>>();
//Read CSV line by line and use the string array as you want
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null) {
//Verifying the read data here
ArrayList<Long> result_01 = new ArrayList<Long>();
for(int k=0;k<nextLine[1].replace("[", "").replace("]", "").replace(" ", "").split(",").length;k++){
result_01.add(Long.parseLong(nextLine[1].replace("[", "").replace("]", "").replace(" ", "").split(",")[k]));
}
result.put(Integer.parseInt(nextLine[0]), result_01);
}
}
}
下面是 Rest api Controller
@RestController
public class HomeController {
@RequestMapping(value="/api/sid",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
public ResponseEntity<Map<String, List<Model>>> getid(@RequestParam("sid") int sid) {
Map<String, List<Model>> Map = new HashMap<String, Object>();
List<Model> model=new List<Model>();
model=get_model();
Map.put("hi",model)
return new ResponseEntity<Map<String, List<Model>>>(Map,HttpStatus.OK);
}
@ResponseBody
public List<Model> get_model(){
List list =new List<Model>();
//here I have to use the treemap
return list;
}
}
我可以在每次 api 为 called.but 时创建树图,而不是只需要创建一次并在响应正文中访问它 get_model method.Any 感谢帮助。
使用单例 bean 即创建另一个 bean 从 csv 文件创建 TreeMap 并在 bean 的成员变量中创建 TreeMap。
@Bean
public class RefData{
public TreeMap<Object> treeMap;
public TreeMap<Object> getData(){
if(this.treeMap == null){
//read csv file & prepare TreeMap & store it in this.treeMap
}
return this.treeMap;
}
}
我有一个 REST API,我想调用一个从 csv 文件创建 TreeMap 的方法,我想将 TreeMap 用于每个 API calls.So 我只想调用方法来设置 TreeMap 并想将 TreeMap 用于其余的 API 调用。
我创建 TreeMap 的方法是
public void createTreeMap(){
CSVReader reader = new CSVReader(new FileReader("C:\Users\result.csv"), ',' , '"' , 1);
TreeMap <Integer,ArrayList<Long>> result=new TreeMap <Integer,ArrayList<Long>>();
//Read CSV line by line and use the string array as you want
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null) {
//Verifying the read data here
ArrayList<Long> result_01 = new ArrayList<Long>();
for(int k=0;k<nextLine[1].replace("[", "").replace("]", "").replace(" ", "").split(",").length;k++){
result_01.add(Long.parseLong(nextLine[1].replace("[", "").replace("]", "").replace(" ", "").split(",")[k]));
}
result.put(Integer.parseInt(nextLine[0]), result_01);
}
}
}
下面是 Rest api Controller
@RestController
public class HomeController {
@RequestMapping(value="/api/sid",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
public ResponseEntity<Map<String, List<Model>>> getid(@RequestParam("sid") int sid) {
Map<String, List<Model>> Map = new HashMap<String, Object>();
List<Model> model=new List<Model>();
model=get_model();
Map.put("hi",model)
return new ResponseEntity<Map<String, List<Model>>>(Map,HttpStatus.OK);
}
@ResponseBody
public List<Model> get_model(){
List list =new List<Model>();
//here I have to use the treemap
return list;
}
}
我可以在每次 api 为 called.but 时创建树图,而不是只需要创建一次并在响应正文中访问它 get_model method.Any 感谢帮助。
使用单例 bean 即创建另一个 bean 从 csv 文件创建 TreeMap 并在 bean 的成员变量中创建 TreeMap。
@Bean
public class RefData{
public TreeMap<Object> treeMap;
public TreeMap<Object> getData(){
if(this.treeMap == null){
//read csv file & prepare TreeMap & store it in this.treeMap
}
return this.treeMap;
}
}