将 Spring Boot Web 应用程序连接到 postgresql 服务器

Posted

技术标签:

【中文标题】将 Spring Boot Web 应用程序连接到 postgresql 服务器【英文标题】:Connecting Spring Boot web app to a postgresql server 【发布时间】:2016-01-02 19:51:34 【问题描述】:

我正在使用 Spring Boot 制作一个 Web 应用程序,并且我通过使用内存数据库 (H2) 获得了我想要的功能,但我无法将它连接到我在计算机上设置的 postgresql 服务器。我在这方面已经有一段时间了,并尝试了一堆不起作用的东西,所以我将所有东西都设置回原来的样子,只是为了让它再次工作。

这是我的 UploadController.java,它处理来自服务器的上传并将其放入我的内存数据库中:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import project.service.MediaFile;
import project.service.MediaFileRepository;

@Controller
public class UploadController 

    @Autowired
    private MediaFileRepository repository;

    @RequestMapping(value = "/uploadmedia", method = RequestMethod.GET)
    public String uploadForm() 
        return "upload";
    

    @RequestMapping(value = "/uploadmedia", method = RequestMethod.POST)
    public String uploadSubmit(@RequestParam(value="files[]") MultipartFile[] files,
                               @RequestParam("tags") String tags, @RequestParam("type") String type)
    
        String[] tagsArray = tags.split("\\s+");
        MultipartFile file;
        String name;
        String tag;
        String path;

        for (int i = 0; i < files.length; i++) 
            file = files[i];
            name = file.getOriginalFilename();
            path = "/Users/johannesthorkell/Developer/spring_prufa/images/" + name;
            System.out.println(name);

            if (!file.isEmpty()) 
                try 
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(new File(path)));
                    stream.write(bytes);
                    stream.close();
                    for (int j = 0; j < tagsArray.length; j++) 
                        tag = tagsArray[j].toLowerCase();
                        repository.save(new MediaFile(name, tag, path, type));
                    
                    System.out.println("Success!");
                 catch (Exception e) 
                    System.out.println("Failure... " + e.getMessage());
                
             else 
                System.out.println("No file");
            
        
        return "upload";
    

...这是我的 MediaFile 类,@Entity 对象:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class MediaFile 

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String name;
    private String tag;
    private String resource;
    private String type;

    protected MediaFile() 

    public MediaFile(String name, String tag, String resource, String type) 
        this.name = name;
        this.tag = tag;
        this.resource = resource;
        this.type = type;
    

    public String getTag() 
        return tag;
    

    @Override
    public String toString() 
        return String.format(
                "MediaFile[id=%d, name='%s', tag='%s', resource='%s', type='%s']",
                id, name, tag, resource, type);
    


...这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>HBV501G</groupId>
    <artifactId>Spring_Web_MVC</artifactId>
    <version>0.1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

...最后,这是 application.properties:

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1 

通过此设置,一切正常。我尝试在我的 application.properties 中添加以下内容:

spring.datasource.url=jdbc:postgresql://localhost/test
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.jdbc.Driver

...向 pom.xml 添加以下依赖项:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1200-jdbc41</version>
</dependency>

...然后我尝试混合和匹配这些东西并阅读教程几个小时无济于事。

编辑:

我删除了一行并添加了另一行(在 Stéphane Nicoll 的建议下),所以现在我的 application.properties 看起来像这样:

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
debug=true

spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=username
spring.datasource.password=password

...我的 pom.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>HBV501G</groupId>
    <artifactId>Spring_Web_MVC</artifactId>
    <version>0.1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1200-jdbc41</version>
        </dependency>

        <!--<dependency>-->
            <!--<groupId>com.h2database</groupId>-->
            <!--<artifactId>h2</artifactId>-->
        <!--</dependency>-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

随着对我的 application.properties 的更改,我的应用程序现在可以运行了!但是当我将 POST 表单提交到 UploadController.java 时出现以下错误:

2015-10-06 11:32:14.878  INFO 22287 --- [           main] project.Application                      : Started Application in 11.897 seconds (JVM running for 12.971)
2015-10-06 11:32:40.263  INFO 22287 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-10-06 11:32:40.264  INFO 22287 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2015-10-06 11:32:40.295  INFO 22287 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms
hopaskipting.pdf
2015-10-06 11:32:49.752 DEBUG 22287 --- [nio-8080-exec-3] org.hibernate.SQL                        : select nextval ('hibernate_sequence')
2015-10-06 11:32:49.760  WARN 22287 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2015-10-06 11:32:49.760 ERROR 22287 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "hibernate_sequence" does not exist
  Position: 17
Failure... could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

我将 GenerationType 更改为 IDENTITY 并收到以下错误:

2015-10-06 12:56:32.496 DEBUG 22746 --- [nio-8080-exec-7] org.hibernate.SQL                        : insert into media_file (name, resource, tag, type) values (?, ?, ?, ?)
2015-10-06 12:56:32.505 DEBUG 22746 --- [nio-8080-exec-7] org.hibernate.SQL                        : insert into media_file (name, resource, tag, type) values (?, ?, ?, ?)
Failure... A different object with the same identifier value was already associated with the session : [project.service.MediaFile#0]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [project.service.MediaFile#0]

【问题讨论】:

您在尝试使用 Postgres 时遇到什么失败? 您不需要指定驱动程序,它是自动检测的。您还应该在启动应用程序时添加“--debug”(或将调试放入application.properties),这将显示自动配置报告。它会准确地告诉您启用/禁用的内容以及原因。 @StépaneNic​​oll 我进行了您建议的更改,现在我的应用程序实际运行了,但是当我尝试将数据放入数据库时​​它仍然失败。我添加了 application.properties 和 pom.xml 作为现在的外观以及我到达主帖的错误,我发布了我的问题的答案,其中包含自动配置报告,因为它不适合我的主帖. @AndyWilkinson 我用收到的错误消息更新了我的帖子(在底部)。 【参考方案1】:

错误的关键部分是:

2015-10-06 11:32:49.760 ERROR 22287 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "hibernate_sequence" does not exist

Hibernate 正在寻找一个名为 hibernate_sequence 的表来支持 MediaFile 上的 @GeneratedValue。您目前已经使用AUTO 对其进行了配置。然后,实际行为会因您使用的数据库而异。

我认为你有几个选择:

    在 Postgres (CREATE SEQUENCE) 中创建一个序列,命名为 hibernate_sequence 更改为使用不同的生成类型,例如GenerationType.IDENTITY

【讨论】:

我将生成类型更改为身份,并根据收到的错误消息更改了我的关系,但我遇到了我在 OP 中放入的另一个错误。我没有正确生成 id 吗? 我认为这最好在一个单独的问题中解决,它与连接到 Postgres 没有任何关系。 我可以吻你...这让我发疯了,再次感谢。如果可以的话,我会更多地支持你:)【参考方案2】:

如果我是你,我会创建自己的 dataSource bean,例如:

@Configuration
public class MyConfig

    @Autowired
    Environment env;

    @Bean
    public DataSource dataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("myapp.dataSource.driver"));
        dataSource.setUrl(env.getProperty("myapp.dataSource.url"));
        dataSource.setUsername(env.getProperty("myapp.dataSource.username"));
        dataSource.setPassword(env.getProperty("myapp.dataSource.password"));
        return dataSource;
    

application.properties:

logging.level. = INFO

myapp.dataSource.driver = 
myapp.dataSource.url = 
myapp.dataSource.username = 
myapp.dataSource.password = 

如果您不想尝试此操作,可以尝试将驱动程序 org.postgresql.jdbc.Driver 更改为 org.postgresql.Driver

没有任何日志很难提供帮助。

【讨论】:

以上是关于将 Spring Boot Web 应用程序连接到 postgresql 服务器的主要内容,如果未能解决你的问题,请参考以下文章

Maven Spring Boot 尝试连接到 MySQL

spring-boot web 应用程序在一段时间后失去连接到 MySQL / RDS 的能力

Spring Boot Starter-Web 尝试在启动时连接到 Mongo

如何将 AWS Elasticache Redis 集群连接到 Spring Boot 应用程序?

无法将 Spring Boot 应用程序连接到 IBM Informix 数据库

如何将我的 Spring Boot 应用程序连接到 Docker 上的 Redis 容器?