Categories: JavaSpring Boot

How to create a new Spring Boot Project

With Spring Initializr you can bootstrap a new Spring Boot application swiftly. Let’s create a fresh project from scratch using just Web and MySQL dependencies.

What we are going to build

We will bootstrap a fresh project with basic dependencies required for a web application.

Requirements

You will need Java SDK v1.8 or higher. If this is your first java project, install Java Development Kit:

$ sudo apt-get install openjdk-8-jdk

To check which version is installed on your computer, run:

$ javac -version

You will be coding in an integrated development environment. Choose the one that supports Java8, JavaScript, HTML, is compatible with Git and enables you to build, debug, and run tests with ease.  For the IDE I recommend IntelliJ IDEA Community Edition, but you can choose Eclipse, Netbeans or whichever you like.

This tutorial’s project is created on Kubuntu – remember to adjust commands if you don’t work on Linux OS.

Setting up the project

Spring Initializr provides a simple web UI allowing you to customize the project, specify its dependencies and download an already bootstrapped application.

Visit start.spring.io/, insert :

  • the name of the project’s  Group – usually it is the inversed domain of your company and is used as a default root package of the app;
  • project’s name into the  Artifact field – if the name of your project is my-app, the generated project will have a MyAppApplication.java class with a main() method to bootstrap the application;
  • short Description of the application.

If you won’t specify Package Name, the value of the Group attribute is used for the root package of the project. In our case, the path to the class with a main() method will look like this:

awesome-project/src/main/java/in/keepgrowing/awesomeproject/AwesomeProjectApplication.java

To select Web and MySQL dependencies, place the cursor in the Search for dependencies field, start typing those names and pick out the packages from the list of matching choices.

You can also explore advanced options of Spring Initializr by clicking the Switch to the full version link below the Generate Project button.

Spring Initializr with example project data

Generate the project and save the zip file on your machine. Go to your project location and unzip the file:

little_pinecone:~/projects/spring_boot$ unzip ~/Downloads/awesome-project.zip

To see the structure of the downloaded demo go to the project directory and list the application files:

$ cd awesome-project/
$ ls -la
.
..
.gitignore
.mvn
mvnw
mvnw.cmd
pom.xml
src

To finally test the project installation, create a simple controller displaying a greeting message:

// awesome-project/src/main/java/in/keepgrowing/awesomeproject/Controller/LandingPageController.java

package in.keepgrowing.awesomeproject.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LandingPageController {

    @RequestMapping("/")
    public String greeting() {
        return "Welcome on the landing page!";
    }
}

You need to debug and run the application. If you chose awesome-project as the name of your application in Spring Initializr, go to AwesomeProjectApplication.java, click the green arrow on the left of the class declaration and select Debug 'AwesomeProject...main()' option:

// awesome-project/src/main/java/in/keepgrowing/awesomeproject/AwesomeProjectApplication.java
AwesomeProjectApplication.java class

You should get the console output with the Tomcat port, mine is:

TomcatWebServer : Tomcat started on port(s):8080.

Visit http://localhost:8080/ in your browser and check for the LandingPageController output: Welcome on the landing page!

You should now store your project in a git repository. Check out this tutorial on starting with git.

Photo by Nick Scheerbart on StockSnap

little_pinecone

Recent Posts

Simplify the management of user roles in Spring Boot

Spring Security allows us to use role-based control to restrict access to API resources. However,…

3 years ago

Create a custom annotation to configure Spring Boot tests

A custom annotation in Spring Boot tests is an easy and flexible way to provide…

3 years ago

Keycloak with Spring Boot #4 – Simple guide for roles and authorities

Delegating user management to Keycloak allows us to better focus on meeting the business needs…

3 years ago

Keycloak with Spring Boot #3 – How to authorize requests in Swagger UI

Swagger offers various methods to authorize requests to our Keycloak secured API. I'll show you…

3 years ago

Keycloak with Spring Boot #2 – Spring Security instead of Keycloak in tests

Configuring our Spring Boot API to use Keycloak as an authentication and authorization server can…

3 years ago

Keycloak with Spring Boot #1 – Configure Spring Security with Keycloak

Keycloak provides simple integration with Spring applications. As a result, we can easily configure our…

3 years ago