【问题标题】:Problems with Postgresql with Spring Boot on Heroku在 Heroku 上使用 Spring Boot 的 Postgresql 问题
【发布时间】:2014-12-13 16:12:51
【问题描述】:

我决定在 Heroku 上试用 Spring Boot,一切正常……嗯,除了数据库!

我尝试了很多不同的方法来让它发挥作用,遵循了我在网上找到的许多不同的方法,也从 StackOverflow 上的问题中找到了很多不同的方法,但似乎没有什么对我有用。 我将只发布我尝试部署的应用程序的最低限度。

下面的部署很好,但是当我尝试发布一些东西时,我得到以下响应:

{"timestamp":1413600470146,"error":"Unsupported Media Type","status":415,"message":"Unsupported Media Type"}

当我尝试 GET 时,我在 Heroku 日志中收到以下异常: 嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: relation "exampleEntity" does not exist
  Position: 109
  at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
  at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
  ....

所以,我有以下 pom.xml:

<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>com.example</groupId>
    <artifactId>project-name</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <jersey.version>2.8</jersey.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>


        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-inmemory</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>1.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

那么我的主课是:

package com.example;

import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.example.config.JerseyConfig;

@EnableAutoConfiguration
@ComponentScan
@EnableJpaRepositories
public class Main {

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
        // our rest resources will be available in the path /rest/*
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
        return registration;
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Main.class).showBanner(false).run(args);
    }

}

我还有一个 JerseyConfig(用于 Jersey 配置):

package com.example.config;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(RequestContextFilter.class);
        packages("com.example");
        register(LoggingFilter.class);
    }
}

还有一个用于数据库的 BasicDataSource:

package com.example.config;

import java.net.URI;
import java.net.URISyntaxException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SimpleDbConfig {

    @Bean
    public DataSource dataSource() throws URISyntaxException {    
        URI dbUri;
        try {
            String username = "username";
            String password = "password";
            String url = "jdbc:postgresql://localhost/dbname";
            String dbProperty = System.getProperty("database.url");
            if(dbProperty != null) {
                dbUri = new URI(dbProperty);

                username = dbUri.getUserInfo().split(":")[0];
                password = dbUri.getUserInfo().split(":")[1];
                url = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();
            }     

            BasicDataSource basicDataSource = new BasicDataSource();
            basicDataSource.setUrl(url);
            basicDataSource.setUsername(username);
            basicDataSource.setPassword(password);
            return basicDataSource;

        } catch (URISyntaxException e) {
            //Deal with errors here.
            throw e;
        }
    }
}

最后,我有了我的 REST 资源:

package com.example.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.service.ExampleService;
import com.example.transport.ExampleEntity;

@Path("/entities")
@Component
public class ExampleResource {

    private ExampleService exampleService;

    @Autowired
    public ExampleResource(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    @POST
    @Path("/new")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ExampleEntity createNewEntity(ExampleEntity entity) {
        return exampleService.createNewEntity(entity);
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public ExampleEntity getExampleDetails(@PathParam("id") Long id) {
        return exampleService.findEntity(id);
    }

}

一个服务类:

package com.example.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.repositories.ExampleRepository;
import com.example.transport.ExampleEntity;

@Service
public class ExampleService {

    private ExampleRepository repo;

    @Autowired
    public ExampleService(ExampleRepository repo) {
        this.repo = repo;
    }

    @Transactional
    public ExampleEntity createNewEntity(ExampleEntity entity) {
        ExampleEntity savedEntity = repo.save(entity);
        return savedEntity;
    }

    @Transactional
    public ExampleEntity findEntity(Long id) {
        return repo.findOne(id);
    }

}

实体:

package com.example.transport;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@Entity
public class ExampleEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

最后是repo接口:

package com.example.repositories;

import org.springframework.data.repository.CrudRepository;

import com.example.transport.ExampleEntity;

public interface MeetingRepository extends CrudRepository<ExampleEntity, Long> {

}

非常感谢任何帮助!

编辑:

我使用的 Procfile 如下:

web: java -Dserver.port=$PORT -Ddatabase.url=$DATABASE_URL $JAVA_OPTS -jar target/project-name-0.0.1-SNAPSHOT.jar

【问题讨论】:

标签: java postgresql heroku spring-boot spring-data-jpa


【解决方案1】:

您似乎还没有创建表exampleEntity。我可以看到您正在使用 postgreSQL,所以请使用 heroku config | grep HEROKU_POSTGRESQL 检查您是否在 heroku 中启用了一个(还有它的名称和 url)。如果没有,请在您的 heroku 帐户管理器中启用它,它是一个免费插件。 使用heroku pg:promote HEROKU_POSTGRESQL_&lt;color of your db&gt; 将其提升为主要的。你会得到一些像这样的输出:

Promoting HEROKU_POSTGRESQL_BROWN_URL (DATABASE_URL) to DATABASE_URL... done 

现在您应该使用 DATABASE_URL 环境变量连接到您的数据库。 Heroku 将为您提供。 (也请发布您的 Procfile)。

使用 url 连接到您的数据库,并使用与您的 ExampleEntity 类匹配的架构添加所需的表。

旁注

连接到 Heroku 数据库:

如果你不理解我写的一些heroku的东西,他们的文档写得很好:

如果我的理解都错了,并且您在 localhost 上运行您的应用程序并且尚未将其部署到 heroku,那么只需将表添加到您的本地数据库实例。

【讨论】:

  • 谢谢你的回答 vic。我启用了 Postgresql 数据库(上述 grep 的结果类似于:HEROKU_POSTGRESQL_WHITE_URL: postgres://:@ec2-50-17-207-54.compute-1.amazonaws.com:5432 /dfjhng7120830i)。我也在使用 DATABASE_URL 来连接它(通过 Procfile 传递它并在 SimpleDbConfig 类中使用它。但我不知道如何创建我想使用的表。我以为它们是自动创建的。怎么能我连接到数据库来创建它们?我可以通过浏览器来创建它们吗?
  • Hibernate 可以自动删除并重新创建您的表,如果您愿意 - 只需 google hibernate autocreate(您真的不想在生产中这样做)。据我所知,您无法使用简单的浏览器连接到数据库,您需要使用数据库客户端。对于 postgreSQL,默认的名为 pgadmin 并且应该随 posgresql 安装一起提供 (...\PostgreSQL\9.2\bin)。我推荐的方法是设置本地数据库(在您的计算机上),创建必要的表(=关系)然后在本地运行您的应用程序。
【解决方案2】:

我使用自动表生成的​​示例

除了始终存在的DATABASE_URL,Heroku 在运行时创建了 3 个环境变量。它们是:

JDBC_DATABASE_URL
JDBC_DATABASE_USERNAME
JDBC_DATABASE_PASSWORD

您可能知道,如果 Spring Boot 在您的 application.properties 文件中找到 spring.datasource.* 属性,它将自动配置您的数据库。这是我的 application.properties 的示例

spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

Hibernate / Postgres 依赖项

就我而言,我使用的是 Hibernate(与 PostgreSQL 捆绑在 spring-boot-starter-jpa 中,所以我需要在我的 build.gradle 中使用正确的依赖项:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile('org.postgresql:postgresql:9.4.1212')
}

【讨论】:

    猜你喜欢
    • 2019-08-03
    • 2020-06-27
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多