2015-10-06 169 views
2

我正在使用Spring Boot製作Web應用程序,並且我使用內存數據庫(H2)的功能,但我無法將其連接到postgresql服務器I設置在我的電腦上。我已經在這裏待了一段時間,並嘗試了一堆無法正常工作的東西,所以我將所有事情都恢復到了只是讓它再次運行的方式。將Spring Boot web應用程序連接到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"; 
    } 
} 

...這是我的媒體文件類中,@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.p roperties:

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> 

...然後我試圖混合和匹配這些東西和閱讀教程幾個小時無濟於事。

編輯:

我刪除一行,並添加另一個(在斯特凡尼科爾的意見),所以現在我的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身份並得到以下錯誤:

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] 
+1

你看到了什麼故障,當您嘗試使用Postgres的? –

+0

您不需要指定驅動程序,它會自動檢測到。當您啓動的應用程序(或把調試中'application.properties'),這將顯示自動配置報告,您還應該添加「--debug」。它會告訴你到底是什麼被啓用/禁用以及爲什麼。 –

+0

@StépaneNic​​oll我做你的修改建議,現在我的應用程序實際運行,但是當我試圖把我的數據在數據庫中仍然失敗。我添加了application.properties和pom.xml作爲現在的外觀以及我得到主帖子的錯誤,並且發佈了一個包含自動配置報告的問題的答案,因爲它不適合我的主要帖子。 –

回答

4

錯誤的關鍵部分是:

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支持@GeneratedValueMediaFile表。您目前使用AUTO進行了配置。實際行爲隨着您使用的數據庫而異。

我認爲你有兩個選擇:

  1. 創建Postgres的序列(CREATE SEQUENCE),命名爲hibernate_sequence
  2. 更改爲使用不同的發電類型,例如GenerationType.IDENTITY
+0

我改變了一代類型的身份,並根據我收到的錯誤消息,改變了我的關係,但我堅持上,我把在OP另一個錯誤。我沒有正確生成ID嗎? –

+0

我認爲這將在一個單獨的問題得到更好的解決,它沒有任何與連接到Postgres的。 –

+0

是啊你是對的,非常感謝你的幫助 –

0

如果我是你,我倒是創建自己的數據源豆,如:

@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

沒有任何日誌很難提供幫助。

相關問題