我正在开发一个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();
}
}
我无法访问数据。感谢任何帮助。
答案 0 :(得分:2)
您需要执行以下步骤:
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服务器中的提取文件夹名称相同
答案 1 :(得分:1)
@约翰
我根据我的数据库扭曲了你的代码,并且能够使用mysql从db.Im获取数据。
以下是代码:
@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;
}
}