2016-12-27 83 views
0

我正在開發一個春季啓動web應用程序(我的第一個應用程序),並且它在我將它部署到嵌入式tomcat服務器時正常工作。但是,當我將它部署在獨立的Tomcat服務器中時, accesss的database.I正在使用WebService的休息將數據傳遞到前端和我的網址會像春季啓動運行但獨立的tomcat不工作

http://localhost:8080/day_demand?day=3 

但在我的獨立服務器,當我訪問 http://localhost:8080/WebApp/day_demand?day=3(Web應用程序是我的項目名稱)

與數據庫的連接由以下代碼構成:

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

我無法訪問data.Any幫助表示讚賞。

+0

你檢查日誌?它是否給出了任何特定的錯誤。 –

回答

1

@約翰

我已經扭曲你的代碼按我的數據庫和即時通訊能夠用mysql從db.Im獲得的數據外Tomcat服務器提取的文件夾名稱。

connection Class ScreenShot

Calling Class ScreenShot

下面是代碼:

   @RestController 
       public class PersonController { 



        @Autowired 
        private PersonRepository personRepository; 

        @RequestMapping(value = "/persons/{id}", method = RequestMethod.GET,produces={MediaType.APPLICATION_XML_VALUE},headers = "Accept=application/xml") 
        public ResponseEntity<?> getPersonDetails(@PathVariable Long id, final HttpServletRequest request)throws Exception { 
         System.out.println("Before"); 
         ConnectionManager cm=new ConnectionManager(); 
         Person personResponse=cm.populateListOfTopics(); 
         System.out.println("personResponse"+personResponse); 
         return ResponseEntity.ok(personResponse); 
        } 

       } 

連接類:

    import java.sql.Connection; 
        import java.sql.DriverManager; 
        import java.sql.PreparedStatement; 
        import java.sql.ResultSet; 
        import java.sql.SQLException; 
        import java.sql.Timestamp; 
        import java.util.List; 

        public class ConnectionManager { 

        private Connection connectToDatabaseOrDie() 
         { 
         Connection conn = null; 
         try 
         { 
          Class.forName("com.mysql.jdbc.Driver"); 
          String url = "jdbc:mysql://localhost:3306/master?createDatabaseIfNotExist=false"; 
          conn = DriverManager.getConnection(url,"root", "mysql"); 
         } 
         catch (ClassNotFoundException e) 
         { 
          e.printStackTrace(); 
          System.exit(1); 
         } 
         catch (SQLException e) 
         { 
          e.printStackTrace(); 
          System.exit(2); 
         } 
         return conn; 
         } 

        public Person populateListOfTopics() 
         { 
         Person person=new Person(); 
         try 
         { 
          Connection conn = ConnectionManager.this.connectToDatabaseOrDie(); 
          String sql= "SELECT * FROM master.person WHERE ID = 1"; 
          PreparedStatement pstmt = conn.prepareStatement(sql); 
          ResultSet rs = pstmt.executeQuery(); 

          while (rs.next()) 
          { 
          person.setFirst_name(rs.getString("FIRST_NAME")); 

          } 

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

         return person; 
         } 


        } 
+0

這是造成問題的postgresql驅動程序依賴項。非常感謝 – John

1

你需要做下面的步驟:

1) In pom.xml file , make scope as provided for embedded server 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-undertow</artifactId> 
      <scope>provided</scope> 
    </dependency> 

    or 

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
</dependency> 

2) In pom.xml file, make packaging as war 

    <packaging>war</packaging> 

3) Extend SpringBootServletInitializer class in your Application.class 


      import org.springframework.boot.SpringApplication; 
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
      import org.springframework.boot.autoconfigure.SpringBootApplication; 
      import org.springframework.boot.builder.SpringApplicationBuilder; 
      import org.springframework.boot.context.web.SpringBootServletInitializer; 
      import org.springframework.context.annotation.ComponentScan; 
      import org.springframework.context.annotation.Configuration; 


      @SpringBootApplication 
      @Configuration 
      @ComponentScan 
      @EnableAutoConfiguration 

      public class Application extends SpringBootServletInitializer{ 



       public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
       } 

       @Override 
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
        return application.sources(Application.class); 
       } 

       private static Class<Application> applicationClass = Application.class; 

      } 



4) Take the war from target folder and deploy it to External tomcat 
    and start the server.You will see logs as below : 

Spring Boot Tomcat Server Logs

Spring Boot Tomcat Server Logs ..continued

5)有些擊中網址,如下所示:

http://localhost:8080/SpringBootExamples-0.0.1-SNAPSHOT/persons/1 

SpringBootExamples-0.0.1 -SNAPSHOT =上下文路徑

+0

問題是應用程序運行在獨立的tomcat服務器,但它不能訪問數據庫。我得到的錯誤** http:// localhost:8080是不允許的訪問控制,允許來源** – John

+0

你可以請粘貼關於如何連接到數據庫的代碼? –