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