java - 在 Heroku 上使用 Spring Boot 的 Postgresql 问题

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

我决定在 Heroku 上试用 Spring Boot,一切都很完美......好吧,除了数据库!

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

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

{"timestamp":1413600470146,"error":"不支持的媒体类型","status":415,"message":"不支持的媒体类型"

当我尝试获取时,我在 Heroku 日志中收到以下异常: 嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet] 的根本原因

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 接口(interface):

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

最佳答案

看来您还没有创建表exampleEntity然而。我可以看到你正在使用 postgreSQL,所以请使用 heroku config | grep HEROKU_POSTGRESQL检查您是否在 heroku 中启用了一个(还有它的名称和 url)。如果没有,请在您的 heroku 客户经理中启用它,它是一个免费插件。 使用 heroku pg:promote HEROKU_POSTGRESQL_<color of your db>将其提升为主要的。你会得到如下输出:

Promoting HEROKU_POSTGRESQL_BROWN_URL (DATABASE_URL) to DATABASE_URL... done 

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

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

边注

连接到 heroku 数据库:

如果你不明白我写的一些 heroku 的东西,他们的文档写得非常好:

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

关于java - 在 Heroku 上使用 Spring Boot 的 Postgresql 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435836/

相关文章:

java - 使用底部导航栏

java - 自 Java 7u13 以来,字体字形不使用 Graphics2D drawString 呈现

java - C# 相当于具有多个 Setter 方法的 Java 类

postgresql - Postgres 大数值运算

ruby-on-rails-3 - 计划删除 SSL : will that solve my slow loading time and will I still be able to have secure login/signup?

java - 改为 Play Framework : type Integer is deprecated: use java. lang.Integer

sql - 在 Postgres 中从另一个物化 View 调用一个物化 View

sql - 如何将实体转换为在 PostgreSQL 中设置

python - Heroku 需要 ALLOWED_HOSTS 吗?

html - 有时我网站的背景 JPEG 无法完全加载。刷新不会重新下载。我能做些什么来防止这种情况发生?