简体   繁体   English

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

[英]Connecting Spring Boot web app to a postgresql server

I'm making a web application using Spring Boot and I have the functionality I want by using a in memory database(H2) but I can't connect it to the postgresql server I set up on my computer. 我正在使用Spring Boot创建一个Web应用程序,并且我通过使用内存数据库(H2)获得了我想要的功能,但我无法将其连接到我在计算机上设置的postgresql服务器。 I've been at this for some time and tried a bunch of stuff that didn't work so I set everything back to the way it was just to get it working again. 我已经有一段时间了,并尝试了一些不起作用的东西,所以我把所有东西都重新设置为让它再次运行的方式。

Here's my UploadController.java, it handles the upload from the server and puts it into my in memory database: 这是我的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";
    }
}

...and here's my MediaFile class, the @Entity object: ...这是我的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);
    }

}

...here's my pom.xml: ...这是我的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>

...and finally, here's the application.properties: ...最后,这是application.properties:

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

With this setup everything works. 有了这个设置,一切正常。 I tried putting the following in my application.properties: 我尝试将以下内容放在我的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

...Along with adding the following dependency to the pom.xml: ...以及向pom.xml添加以下依赖项:

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

...And then I tried mixing and matching these things and reading tutorials for a few hours to no avail. ...然后我尝试混合和匹配这些东西和阅读教程几个小时无济于事。

Edit: 编辑:

I removed one line and added another (at the advice of Stéphane Nicoll) so now my application.properties look like this: 我删除了一行并添加了另一行(根据StéphaneNic​​oll的建议)所以现在我的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

...And my pom.xml looks like this: ...而我的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>

With the changes to my application.properties my app now runs! 随着我的application.properties的更改我的应用程序现在运行! But I get the following error when I submit to my POST form to UploadController.java: 但是当我将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

I changed the GenerationType to IDENTITY and get the following error: 我将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]

The key part of the error is: 错误的关键部分是:

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's looking for a table named hibernate_sequence to support @GeneratedValue on MediaFile . Hibernate正在寻找一个名为hibernate_sequence的表来支持MediaFile @GeneratedValue You've configured it with AUTO at the moment. 您目前已使用AUTO配置它。 The actual behaviour then varies depending on the database you're using. 然后,实际行为会根据您使用的数据库而有所不同。

I think you have a couple of options: 我想你有几个选择:

  1. Create a sequence in Postgres ( CREATE SEQUENCE ), named hibernate_sequence 在Postgres( CREATE SEQUENCE )中创建一个名为hibernate_sequence CREATE SEQUENCE
  2. Change to using a different generation type, eg GenerationType.IDENTITY 更改为使用其他生成类型,例如GenerationType.IDENTITY

If I were you, I'd create my own dataSource bean, like: 如果我是你,我会创建自己的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: application.properties:

logging.level. = INFO

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

IF you don't want to try this, you could try changing your driver org.postgresql.jdbc.Driver to org.postgresql.Driver . 如果您不想尝试此操作,可以尝试将驱动程序org.postgresql.jdbc.Driver更改为org.postgresql.Driver

It's hard to help without any logs. 没有任何日志,很难帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM