2016-12-28 19 views
1

我開發一個春天啓動應用程序,這是我的休息控制器總是返回emptyJSON陣列在獨立的Tomcat

@RestController 
public class State_Controller { 
    List<State> state_list = new ArrayList<State>(); 
    state_data object; 

    @RequestMapping(value="/state",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET) 
    public ResponseEntity<List<State>> getthreshold(@RequestParam("start") String start_time,@RequestParam("end") String end_time){ 

      Timestamp start_time1 = Timestamp.valueOf(start_time); 
      Timestamp end_time1 = Timestamp.valueOf(end_time); 

     List<State> state_list=get_state(start_time1, end_time1); 
     return new ResponseEntity<List<State>>(state_list,HttpStatus.OK); 
    } 
    @ResponseBody 
    public List<State> get_state(Timestamp start_time,Timestamp end_time){ 

     System.out.println(start_time); 
     object = new state_data(start_time,end_time); 
     state_list=object.printTopics(); 
     return state_list; 
    } 
} 

我的數據庫連接,並返回結果集的方法:

private Connection connectToDatabaseOrDie() 
    { 
    Connection conn = null; 
    try 
    { 
     Class.forName("org.postgresql.Driver"); 
     String url = "jdbc:postgresql://localhost:5432/data_base"; 
     conn = DriverManager.getConnection(url,"user", "password"); 
    } 
    catch (ClassNotFoundException e) 
    { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    catch (SQLException e) 
    { 
     e.printStackTrace(); 
     System.exit(2); 
    } 
    return conn; 
    } 
private void populateListOfTopics(Connection conn, List<State> listOfBlogs,Timestamp start_time,Timestamp end_time,int zone_id) 
    { 
    try 
    { 

     String sql= "SELECT * FROM public.table where time >= ? and time <= ?"; 
     PreparedStatement pstmt = conn.prepareStatement(sql); 
     pstmt.setTimestamp(1,start_time); 


     pstmt.setTimestamp(2,end_time); 


     ResultSet rs = pstmt.executeQuery(); 


     while (rs.next()) 
     { 
      State blog = new State(); 

     blog.year = rs.getInt ("year"); 
     blog.month=rs.getInt ("month"); 
     blog.day = rs.getInt ("day"); 
     blog.hour = rs.getInt ("hour"); 



     listOfBlogs.add(blog); 
     } 

     rs.close(); 
     pstmt.close(); 
     conn.close(); 
    } 
    catch (SQLException se) { 
     System.err.println("Threw a SQLException creating the list of state."); 
     System.err.println(se.getMessage()); 
    } catch (Exception e) { 
     System.out.println("Err"); 
     e.printStackTrace(); 
    } 
    } 

所以問題是當我訪問的網址

http://localhost:8080/state?start=2012-03-03 00:00:00&end=2012-03-03 10:00:00 

當我使用嵌入式tomcat它給了J SONdata perfectly.But如果我在獨立的Tomcat部署應用程序,然後

http://localhost:8080/Application_Name/state?start=2012-03-03 00:00:00&end=2012-03-03 10:00:00 

返回每次一個空數組。 任何幫助表示讚賞。

回答

1

我想通了什麼問題。這個問題發現在postgresql jdbc連接器的依賴不起作用。