Advertisement

header ads

Spring boot tutorial - Quick introduction



This is our Spring boot quick tutorial for beginners. Spring Boot is an java based framework which uses to develop microservices. This allows us to create java base web applications more efficiently. If you don't have an idea about microservices, you can refer this article to get a clear idea about microservices. Spring boot is built on top of the Spring framework. Spring boot provide an easy access to the Spring framework. If you are confused with Spring framework and Spring boot, lets think the Spring framework contains the ingredients to make a cup of tea. That means it contains sugar, milk and tea. Then you can say the spring boot is the completed tea.

Characteristics of Spring boot.

  • Its not an APP or a WEB server
  • Does not generate any code
  • Provides a range of none-functional features

Spring vs Spring boot

Spring Spring Boot
Dependency injection framework Pre configured set of frameworks
Manages lifecycles of java classes No need to worry about managing lifecycles
Takes a lot of time to run the APP       Shortest method to run the APP

Apache Maven - a spring boot companion

Apache Maven is a build automation tool for java projects. This tool makes creating spring boot applications a lot easier. You will understand the advantages of this tool when we creating our project.

Creating a simple spring boot web application

Lets create a simple project to understand the basics of spring boot
  • Operating system: Linux (ubuntu 17)
  • IDE: eclipse oxygen
  • Maven version: 3.5 (install guide)
  • java version: 1.8
i) Open eclipse IDE and go to new-maven project
ii) Now select Create Simple Project and Allow to use default workspace (this helps to make our first project easily)
iii) Click next. Then you will get following screen.
In here you can see couple of configurations. Most of these are already set and we just have to add following parameters.
  • Group Id: this is similar to the package names when creating normal java project. Therefore add a group Id whatever you like according to given model (xxx.xxx.xxx). Its a good practice.
  • Artifact ID: is similar to the class name.
  • Description: can add any description about the project

iii) That's all we need to fill and then click on Finish.
Now you will have a maven project with following components.
Lets consider the main components of above project
  • src/main/java: contains source code of the project
  • xx/mainsources: contains resource files such as .json files
  • xx/test/java: contains test files such as junit tests.
  • pom.xml: contains all dependencies of the project .

Steps to build a complete spring boot web application

We just created our spring boot project using maven. But this project is not yet functioning, or simply we cannot get anything from this. There are some steps to build a complete spring boot application using maven. Let's take a look at following diagram.
Steps 1-4 contains all configurations that we have to make in order to complete our spring boot application. All these configurations are made to our pom.xml file. Step 4 is creating a java class with the main method and we will get into that later.

1. Adding spring boot starter parent

This provide default packages which are useful to spring applications.
  • You can get various releases using this link
  • Open your pom.xml file
  • Create <parent></parent> tags between '<project> </project> ' tags
  • Add starter parent between 'parent' tags

Note: You will see that in the maven repository, it is in between the <dependency> tags, but since this is your first spring boot app and you need to get a well structured code, use following method.

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.1.RELEASE</version>

 </parent>

  • Then save the project

2. Adding spring boot web starter

This provides all neccessary packages to develop web applications

  • Go to this link and select a suitable version.
  • Then add the dependency between dependencies tags in the pom.xml file.

Note: all dependency versions have to be the same as the parent version. Therefore you don't need to specify the version for each dependency after you have added the 'starter parent' as in step 1.
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Now you will be able to see that a folder named 'Maven Dependencies' has been added to the project.
This is the advantage of using maven to build spring boot project. If we don't use maven, we have to add all .jar files one by one to this 'Maven Dependency' folder manually. But when using maven, we just want to update the 'pom' file. Maven tool will automatically download and add .jar files for us.
Note: If you cannot see the 'Maven Dependencies' folder, right click on the project name > select 'Maven' > select 'Update Project'

3. Configuring java version

You can specify the java version for your project by simply adding a property for the pom file as follows.
 <properties>
  <java.version>1.8</java.version>
 </properties>

4. Adding spring boot plugin

This dependancy provide support to develop Spring boot application using maven tool

  • Go to this link and add the dependancy to your pom file as follows

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>2.1.1.RELEASE</version>
</dependency>
Now our 'pom.xml' file is ready !

Final pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>info.techmagister.springBootDemo</groupId>
 <artifactId>SpringBootDemo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <description>Spring boot example</description>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.1.RELEASE</version>

 </parent>
 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <version>2.1.1.RELEASE</version>
  </dependency>
 </dependencies>
 <properties>
  <java.version>1.8</java.version>
 </properties>
</project>

5. Creating spring boot application launcher.

This is  basically a java class which allows us to run our spring boot project as a java application.

  • Go to src/main/java folder and create a new package 'info.techmagister.SpringBootDemo'
  • Then create a new java class named 'Application.java'
  • Then add following code to the class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {
 public static void main(String[] args) {
  ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
 
 }

}
Note: Please be aware about the imports in line 1,2 and 3. You have to use same imports.

Lets consider above code:
@SpringBootApplicaton - This annotation helps to identify that this is and Spring boot application.
ApplicationContext - This provides configuration information to the application. (you can read more from this link)

Now our main method is ready. But we have to have a controller which manages the user requests to the application. Create a java class named 'HelloWorldController.java' and add following code.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
 @RequestMapping("/hello")
 public String HelloWorld() {
  return "Hello World";

 }

}
Note: Please be aware about the imports in line 1 and 2. You have to use same imports.

Lest consider above code:
@RestController - Helps to identify that this is a RESTful web service (read more)
@RequestMapping - Matches the "/hello" url to the 'HelloWorld' method.

Now we have completed developing our spring boot application. This is my complete project structure.


Now right click on the project folder ('SpringBootDemo') and run as java application. Now check the console window and you will be able to see the application has started in port 8080 (port number may differ).
Then go to the browser and type 'http://localhost:8080/hello' and you will get the 'Hello World' message which is the return statement of our controller method.
If you can get the same response as above, you have completed your first spring boot project successfully. Now you can try different examples to get a wider knowledge on spring boot. If you are getting errors, you can check the console and find out the real cause. Also you can make a comment below of you have anything to clarify. Like our facebook page and share this article among others. See you in the next tutorial.

Post a Comment

0 Comments