javascript - spring boot 运行但独立的 tomcat 不工作

标签 javascript jquery web-services tomcat spring-boot

我正在开发一个 spring boot web 应用程序(我的第一个应用程序),当我将它部署在嵌入式 tomcat 服务器时它工作正常。但是当我将它部署在独立的 Tomcat 服务器时它无法访问数据库。我正在使用 Rest WebService 将数据传递到前端,我的 url 看起来像

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

但是在我的独立服务器上访问时 http://localhost:8080/WebApp/day_demand?day=3(WebApp是我的项目名)

通过以下代码连接到数据库:

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

我无法访问数据。感谢任何帮助。

最佳答案

您需要执行以下步骤:

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) 像下面这样点击 URL :

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

SpringBootExamples-0.0.1-SNAPSHOT = 上下文路径

与外部 Tomcat 服务器中提取的文件夹名称相同

关于javascript - spring boot 运行但独立的 tomcat 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41339058/

相关文章:

javascript - 访问元素值

javascript - 如何更改 css 下拉列表以根据文本大小更改大小?

jquery - CSS 的定位问题

javascript - 当 mouseleave 在 slideffect 时间内文本停留

java - 为什么我在 CXF 中收到此错误

javascript - 在Grails中将JSON对象呈现为AJAX调用

javascript - 在 'document.execCommand' 键事件上执行 'delete' - AngularJS

web-services - TLS 1.0 服务器和 TLS 1.1 服务器之间的 Web 服务

javascript - 在 jQuery 中创建 CSS 类

web-services - Web 框架在读取正文之前是否检查授权 header ?