Hi everyone, today we're writing a hello world project in spring-boot application. I had always loved java as a programming language, and I wanted to expand my skill set in the domain. Recently, we were given a task to write a basic application of CRUD operations in spring boot, but let's keep that for another day.
Today, we'll learn how to start with this amazing technology framework. Let's begin!
Tools you need:
-
java version 1.7 or above: You can check that by writing
java -version
inside your terminal or command prompt. - Eclipse IDE: Eclipse download Go for Java EE version.
-
Maven or Gradle: For this tutorial, I'll be going with Maven. Check your maven version
mvn -v
Here's the screenshot for reference:
Steps to follow:
- Go to Spring starter kit. It is by far the most comprehensive way to get started with a spring application.
- Fill in group, artifact ID (most probably, your project name) and description (optional). Also, don't forget to check the spring version, although you need not to worry as it is checked to the latest stable one by default.
Add dependencies, by clicking the button on right top side. Type in web, then select spring web. Have a look at below screenshots for reference;
Click on generate, you'll see a .zip getting download with your artifact ID.
Extract the file and open Eclipse.
Import the project using below steps:
File -> Import -> Existing Maven Project (you can type in the search box too) -> Next -> Browse -> Select the Project Folder -> FinishNow you should see your project in your project explorer as below:
Now that we've our main application template ready, we need a controller class that'll control our application, as to what should my application should render or do upon handling a request. Basically, A controller controls the flow of the data. It controls the data flow into model object and updates the view whenever data changes.
-
For this to achieve we create a package to associate our controller classes in a single package for easy and practical access. So, go to your project explorer and make a new package inside your
src/main/java
Now,give it a name and write your controller class as given below:
- Now let's add controller functions and annotations to give it a meaning.
You can see the annotations
RestController
andRequestMapping
RestController specifies that a RESTful web service is being created. RequestMapping maps the URL associated with the application and renders the function as per the requested URL. In this we've specified just the landing page("/")
- Start the project by clicking run as > java application. Alternatively, you can choose spring boot suite in eclipse marketplace by which you can start and debug the application easily. You'll see the hello world screen. Go on and have a look at localhost:8080
Top comments (0)