0

I am in the process of uploading an application I am running on my local PC to Heroku. It has a Springboot backend accessing a Postgresql database and a React\Axios front end.

I uploaded the source to GITHUB and created a project in Heroku and deployed from Heroku. I created the Postgresql database, deployed the Backend and Front end. The React Front end executes and paints the screen, but no data is sent from the backend.

Springboot Main

package net.javaguides.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class SpringbootBackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootBackendApplication.class, args);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080","http://localhost:3000","http://rmsystem.ddns.net:3000")
                .allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS");
            }
        };
    }
    
}

I can connect from my PC to the remote database using PgAdmin. I can also run the Springboot Backend and React Frontend locally and access the remote Heroku Postgres database (wow). POSTMAN works locally to display data from the remote backend.

I tried to add a Datasource.java file under src\main\resources
Does not seem to do anything.

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class DataSource {
    @Bean
    public DriverManagerDataSource dataSource(){
        DriverManagerDataSource source = new DriverManagerDataSource();
        source.setDriverClassName("org.postgresql.Driver");
        source.setUrl("jdbc:postgres://xmmazemfflgbkj:a323bc337c559fffd0ec7029daaf60f03712cdb1df41ca8629fe8429f0a7a607@ec2-54-85-113-73.compute-1.amazonaws.com:5432/d423nfcf5dbjf7");
        source.setUsername("xmmazemfflgbkj");
        source.setPassword("a323bc337c559fffd0ec7029daaf60f03712cdb1df41ca8629fe8429f0a7a607");
        return source;
    }
    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource());
        return namedParameterJdbcTemplate;
    }
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion> 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.javaguides</groupId>
    <artifactId>budget-backend-postsql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>budget-backend-postsql</name>
    <description>Budget Project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                  <mainClass>com.places.Main</mainClass>
                 </configuration>
                 <executions>
                  <execution>
                    <goals>
                      <goal>repackage</goal>
                    </goals>
                  </execution>
                 </executions>  
            </plugin>
        </plugins>
    </build>

</project>

Springboot Application.Properties:

spring.datasource.url=jdbc:postgresql://ec2-54-85-113-73.compute-1.amazonaws.com:5432/d423nfcf5dbjf7
spring.datasource.username=xmmazemfflgbkj
spring.datasource.password=a323bc337c559fffd0ec7029daaf60f03712cdb1df41ca8629fe8429f0a7a607
spring.datasource.driver-class-name=org.postgresql.Driver
server.port=${PORT:8080}

When I deploy the Springboot Backend from my Github repository, Heroku says Application Successfully Deployed'.

Complete Build Log

-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/java
-----> Java app detected
-----> Installing JDK 11... done
-----> Executing Maven
       $ ./mvnw -DskipTests clean dependency:list install
       [INFO] Scanning for projects...
       [INFO] 
       [INFO] ---------------< net.javaguides:budget-backend-postsql >----------------
       [INFO] Building budget-backend-postsql 0.0.1-SNAPSHOT
       [INFO] --------------------------------[ jar ]---------------------------------
       [INFO] 
       [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ budget-backend-postsql ---
       [INFO] 
       [INFO] --- maven-dependency-plugin:3.1.2:list (default-cli) @ budget-backend-postsql ---
       [INFO] 
       [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ budget-backend-postsql ---
       [INFO] Using 'UTF-8' encoding to copy filtered resources.
       [INFO] Using 'UTF-8' encoding to copy filtered properties files.
       [INFO] Copying 1 resource
       [INFO] Copying 1 resource
       [INFO] 
       [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ budget-backend-postsql ---
       [INFO] Changes detected - recompiling the module!
       [INFO] Compiling 22 source files to /tmp/build_39c5c180/target/classes
       [INFO] 
       [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ budget-backend-postsql ---
       [INFO] Using 'UTF-8' encoding to copy filtered resources.
       [INFO] Using 'UTF-8' encoding to copy filtered properties files.
       [INFO] skip non existing resourceDirectory /tmp/build_39c5c180/src/test/resources
       [INFO] 
       [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ budget-backend-postsql ---
       [INFO] Changes detected - recompiling the module!
       [INFO] Compiling 1 source file to /tmp/build_39c5c180/target/test-classes
       [INFO] 
       [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ budget-backend-postsql ---
       [INFO] Tests are skipped.
       [INFO] 
       [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ budget-backend-postsql ---
       [INFO] Building jar: /tmp/build_39c5c180/target/budget-backend-postsql-0.0.1-SNAPSHOT.jar
       [INFO] 
       [INFO] --- spring-boot-maven-plugin:2.5.0:repackage (repackage) @ budget-backend-postsql ---
       [INFO] Replacing main artifact with repackaged archive
       [INFO] 
       [INFO] --- spring-boot-maven-plugin:2.5.0:repackage (default) @ budget-backend-postsql ---
       [INFO] Replacing main artifact with repackaged archive
       [INFO] 
       [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ budget-backend-postsql ---
       [INFO] Installing /tmp/build_39c5c180/target/budget-backend-postsql-0.0.1-SNAPSHOT.jar to /tmp/codon/tmp/cache/.m2/repository/net/javaguides/budget-backend-postsql/0.0.1-SNAPSHOT/budget-backend-postsql-0.0.1-SNAPSHOT.jar
       [INFO] Installing /tmp/build_39c5c180/pom.xml to /tmp/codon/tmp/cache/.m2/repository/net/javaguides/budget-backend-postsql/0.0.1-SNAPSHOT/budget-backend-postsql-0.0.1-SNAPSHOT.pom
       [INFO] ------------------------------------------------------------------------
       [INFO] BUILD SUCCESS
       [INFO] ------------------------------------------------------------------------
       [INFO] Total time:  5.451 s
       [INFO] Finished at: 2022-04-07T16:05:05Z
       [INFO] ------------------------------------------------------------------------
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 91.9M
-----> Launching...
       Released v19
       https://budget-program-backend.herokuapp.com/ deployed to Heroku

Click Open APP, get err indictor, here is log from opening app.

2022-04-07T16:05:25.936640+00:00 app[web.1]:    at java.base/java.lang.Class.forName0(Native Method)
2022-04-07T16:05:25.936704+00:00 app[web.1]:    at java.base/java.lang.Class.forName(Class.java:398)
2022-04-07T16:05:25.936759+00:00 app[web.1]:    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
2022-04-07T16:05:25.936813+00:00 app[web.1]:    at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
2022-04-07T16:05:25.936866+00:00 app[web.1]:    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
2022-04-07T16:05:25.936920+00:00 app[web.1]:    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
2022-04-07T16:05:26.079615+00:00 heroku[web.1]: Process exited with status 1
2022-04-07T16:05:26.140845+00:00 heroku[web.1]: State changed from starting to crashed
2022-04-07T16:07:24.801800+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=budget-program-backend.herokuapp.com request_id=162d7ce0-822c-4578-8326-bc39a74b25cf fwd="35.136.227.81" dyno= connect= service= status=503 bytes= protocol=https
2022-04-07T16:07:25.042311+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=budget-program-backend.herokuapp.com request_id=ed6f3924-f8da-449f-8366-fed58d58fe98 fwd="35.136.227.81" dyno= connect= service= status=503 bytes= protocol=https
HighwayRob
  • 119
  • 2
  • 11

1 Answers1

0

You need spring.datasource.driver-class-name=org.postgresql.Driver in your application.properties

  • OK..we are moving in a direction. I added the above code and get different err message. Full message added to original Question Post above. java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgres://xmmazemfflgbkj:a323bc337c559fffd0ec7029daaf60f03712cdb1df41ca8629fe8429f0a7a607@ec2-54-85-113-73.compute-1.amazonaws.com:5432/d423nfcf5dbjf7 @Marko Tripkovic – HighwayRob Mar 31 '22 at 13:30
  • Try to replace jdbc:postgres: with jdbc:postgresql, there is a similar question https://stackoverflow.com/questions/34741443/hikaricp-postgresql-driver-claims-to-not-accept-jdbc-url – Marko Tripkovic Apr 01 '22 at 06:47
  • I made changes as specified (see original post) and get error: `org.postgresql.util.PSQLException: The connection attempt failed. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) ~[postgresql-42.2.20.jar:42.2.20] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.0.jar:2.5.0] . . . Caused by: java.net.UnknownHostException: xmmazemfflgbkj:a323bc337c559fffd0ec7029daaf60f03712cdb1df41ca8629fe8429f0a7a607@ec2-54-85-113-73.compute-1.amazonaws.com` – HighwayRob Apr 01 '22 at 20:43
  • Try to connect to the PostgresSQL server via PgAdmint just to make sure you are using the correct connection URL. – Marko Tripkovic Apr 04 '22 at 08:33
  • OKay. got PgAdmin to connect to Heroku backend! Now let's see if I can tweak my Springboot backend to connect ! ` HostName : ec2-54-85-113-73.compute-1.amazonaws.com Port 5432 Maint. dbase: d423nfcf5dbjf7 Username: xmmazemfflgbkj db Restriction: d423nfcf5dbjf7 ` @Marko Tripkovic – HighwayRob Apr 04 '22 at 16:49
  • I changed the Application.properties (see original post) to reflect what worked in pgAdmin to access remote database from local PC. Now I get new error message: code=H10 desc="App crashed" method=GET path="/" Full error message in original post. Also refers to Missing Main ???? @Marko Tripkovic – HighwayRob Apr 04 '22 at 19:30
  • Did you try to run your spring boot application locally with remote PostgreSQL? – Marko Tripkovic Apr 05 '22 at 06:17
  • It works running Springboot Backend and React Frontend locally accessing remote Postgres database on Heroku (pretty amazing). I refreshed my GITHUB repository and redeployed and still get error (see original post for updated error). So close to resolution. Would you be interested in accessing my GITHUB and Heroku account? This is a test learning process, so all support is appreciated beyond words. – HighwayRob Apr 05 '22 at 13:31
  • I (we) are close! See last comment @Marko Tripkovic – HighwayRob Apr 05 '22 at 13:53
  • How did you deployed spring boot application on heroku?Did you check heroku documentation? – Marko Tripkovic Apr 05 '22 at 16:14
  • I tried to follow the Heroku documentation. I added the Port reference as suggested. I will take another look at the documentation. They are not always 100% clear on what is required or where to place the code. Once I get one working on Heroku, the next will be easy. I am open to suggestions. @Marko Tripkovic – HighwayRob Apr 05 '22 at 18:05
  • Can you please share documentation ulr? – Marko Tripkovic Apr 05 '22 at 19:29
  • https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku I have not tried adding a DatabaseConfig.java file as specified. I will try this morning. The following lines creates errors so I did not include in my code: `spring.datasource.maxActive=10 spring.datasource.maxIdle=5 spring.datasource.minIdle=2 spring.datasource.initialSize=5 spring.datasource.removeAbandoned=true` – HighwayRob Apr 06 '22 at 13:23
  • https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku I added the DatabaseConfig.java file as specified. Springboot would not accept dashes in 'package com.herokuapp.budget-program-backend;' so I used 'package com.herokuapp.budgetprogrambackend;' The following lines creates errors so I did not include in my code: `spring.datasource.maxActive=10 spring.datasource.maxIdle=5 spring.datasource.minIdle=2 spring.datasource.initialSize=5 spring.datasource.removeAbandoned=true` Still get same error. – HighwayRob Apr 06 '22 at 13:37
  • Can you please copy all text from the logs, because we need more information about the application crash. – Marko Tripkovic Apr 07 '22 at 07:45
  • ADded complete build log and crash log to original post. @Marko Tripkovic – HighwayRob Apr 07 '22 at 16:11