2017-04-18 2 views
0

나는 REST API가 있고 csv 파일에서 TreeMap을 만드는 메서드를 호출하고 각 API 호출의 나머지 부분에 대해 TreeMap을 사용하려고합니다. 메소드를 호출하여 TreeMap을 설정하고 나머지 API 호출에 대해 TreeMap을 사용하려고합니다. 그래서 아래컨트롤러에서 한 번만 calla 메서드하는 방법

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); 



         } 
        } 

} 

내가 나무는 API가 내가 대신 called.but 때마다지도를 만들 수 있습니다

@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를 컨트롤러에게 있습니다 한 번만 생성하고 응답 본문 get_model 메소드에 액세스해야합니다. 모든 도움을 주시면 감사하겠습니다.

+0

캐시 사용은 어떻게됩니까? –

+0

어디에서 캐시를 정의해야합니까? – RKR

+0

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html –

답변

1

싱글 톤 빈을 사용합니다. 즉, 다른 빈을 만들어 csv 파일에서 TreeMap을 만들고 TreeMap을 Bean의 멤버 변수에 만듭니다.

@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; 
    } 
} 
+0

그래서이 콩은 컨트롤러 자체에있을 것입니다. – RKR

+1

다른 클래스를 가지고 컨트롤러에 주입하는 것이 좋을 것입니다. – JRR