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.
We will bootstrap a fresh project with basic dependencies required for a web application.
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.
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 :
Group
– usually it is the inversed domain of your company and is used as a default root package of the app;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;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.
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
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
Spring Security allows us to use role-based control to restrict access to API resources. However,…
A custom annotation in Spring Boot tests is an easy and flexible way to provide…
Delegating user management to Keycloak allows us to better focus on meeting the business needs…
Swagger offers various methods to authorize requests to our Keycloak secured API. I'll show you…
Configuring our Spring Boot API to use Keycloak as an authentication and authorization server can…
Keycloak provides simple integration with Spring applications. As a result, we can easily configure our…