我正在开发一个 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 :
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/