Java Spring Boot to Heroku in 5 Minutes

Kecci
4 min readJan 28, 2021

--

Java, Spring boot, RESTful, H2 Database, Swagger, Heroku

There are 5 step to accomplish this project:

  1. Setup Project (Spring Initializr, Dependency POM.xml, Project Structure)
  2. Setup Database (application.properties, check database with h2 console)
  3. Create Object & Component (Entity, Repository, Pojo, Service & Controller)
  4. Setup Swagger Config (Customizing Config)
  5. Setup Heroku (Create App, Deploy App)

Step 1 — Setup Project

Initialize your project with Spring Initializr A Spring Initializr. Open: https://start.spring.io/

Add Dependency through Spring Initializr:

  • Spring Web
  • Spring Data JPA

Setup dependencies in pom.xml

After Download the project, Add this dependency to POM.xml:

<depenencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</depenencies>

Setup folder directories:

└── hello
├── config
├── controller
├── entity
│ ├── dao
│ └── pojo
├── repository
└── service

Setup project name & SDK version

Setup project structure

Step 2 — Setup Database

Setup application.properties

Add this config to your application.properties :

## Server Port
server.port=9090
## H2 Console Dashboard
spring.h2.console.enabled=true
## H2 DataSource
spring.datasource.url=jdbc:h2:mem:db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
## JPA/Hibernate
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

Start application & Check Database via H2 Console.

Setup Database with H2 Console

Start your project, then open: http://localhost:9090/h2-console/

You will see this console:

Step 3 — Setup Object & Component

Layering Object Oriented Design

Setup Object & Component

Create User Entity (in folder entity/dao)

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private Double salary;
//Getter Setter
}

Create User Repository (in folder repository)

public interface UserRepository extends JpaRepository<User, Integer> {
}

Create user Param Pojo (in folder entity/pojo)

public class UserParam {private String name;
private Double salary;
//Getter Setter
}

Create User Service (in folder service)

@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getUserList() {
return userRepository.findAll();
}
public User addUser(UserParam param) {
User user = new User();
user.setName(param.getName());
user.setSalary(param.getSalary());
return userRepository.save(user);
}
}

Create User Controller (in folder controller)

@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getUserList() {
return userService.getUserList();
}
@PostMapping
public User addUser(
@RequestBody UserParam user
) {
return userService.addUser(user);
}
}

Step 4 — Setup Swagger Config

Create Swagger Config (in folder config)

@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.world.hello.controller"))
.paths(regex("/api.*"))
.build();
}
}

Create Swagger Config Customize (Advanced)

@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.world.hello.controller"))
.paths(PathSelectors.ant("/api.*"))
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfo(
"MS Posts : Spring Boot REST API",
"Spring Boot REST API for Posts management",
"1.0",
"Terms of service",
new Contact("Azhwani", "https://devwithus.com", "azhwani@devwithus.com"),
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());
}
}

Step 5 — Setup Heroku

  • We use heroku to Deploy to server.
  • First, we open Heroku Dashboard.
  • Create New App

Then, create new app and follow this step

Install the Heroku CLI

Download and install the Heroku CLI. If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.

$ heroku login

Create a new Git repository

Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a spring-to-heroku

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it better"
$ git push heroku master
Push from other branch
$ git push heroku <branch>:master

Existing Git repository

For existing repositories, simply add the heroku remote

$ heroku git:remote -a spring-to-heroku

Live to Production

You can open example project on: http://spring-to-heroku.herokuapp.com/swagger-ui.html

See this project on github: https://github.com/abyanjksatu/spring-to-heroku

Thank You !

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kecci
Kecci

Written by Kecci

Coders + Librarian @Github

No responses yet

Write a response