So I'm a beginner of spring boot learning it by doing. Here will be only steps (not much theory) of creating spring boot app. Even though I've read theory of spring boot I rarely know what to do next when I actually start making spring boot app.
So in this I'll write what to do, how to do and why we do it? With very little Theory. Now as most beginners do, I'm going to create simple web app and these steps are based on it.
1.Application class (@SpringBootApplication): -
When you first create a project, you automatically get this class generated. It's mostly your application name+ Application (eg, For Taskmanager its TaskmanagerApplication).
It is annotated with @SpringBootApllication annotation which I've talked about in previous blog.
This app is starting point of your application. It has main method in it Springapplication.run() method which bootstraps and launches your app.
2.Controller -
Second thing you create is most of the time is a controller class. A Java class annotated with @Controller/@RestController. This class contains handler methods which receives client request performs some action and returns the response.
Make sure you create a package named Controller inside the package in which your main class with @SpringBootApplication is.
3.Request Handling Methods -
These are annotated with @GetMapping and similar annotations with url path mapped in it. Any request coming from that url is handled by this method.
For starting, we will just return "Hello, World!" and it'll be sent back as response and displayed in browser.
Now Spring boot automatically configures your server which is tomcat and it runs on port 8080 so go on http://localhost:8080/hello or any endpoint you have created and you'll see the hello world message.
Because your application class is annotated with @SpringBootApplication it includes component-scan, which checks for components inside the current package and its sub packages by help of annotations. @Controller is also one of those components annotations. So Springboot creates the TaskController object and registers it inside context so spring can manage it.
Thats it for today. You can check theory of few things like application context/contianer, SpringApplication.run() method, how spring configures application, web MVC, request response rendering, client-server communication etc.

Top comments (0)