This tutorial is another article in the How to setup project series. This tutorial describes how to create a Spring Boot application. It will be a Spring Boot project with Maven and generated by Spring Initializr. This is the best way to create a Spring application, and Spring Initializr provides you a great tool for setup a spring app. In this tutorial, you will learn how to create HTTP endpoints, how to expose HTTP endpoints, how to run spring application with the command line.

Create a project with Spring boot and Maven
Banner


Prerequisites:

To complete this tutorial, you will need to have below things:


Setup the Spring Boot App with Spring Initializr :

In this tutorial, we will setup the project with the help of Spring Initializr Web UI.

Step 1 :

Go to Spring Initializr.

Spring Initializr Web UI
Spring Initializr Web UI
Step 2 :

Select Project as Maven Project, Language as Java, Spring Boot as 2.3.4.

Project, language, and Spring Boot version selection
Project, language, and Spring Boot version selection
Step 3 :

Enter details in Project Metadata.

Step 4:

Select Spring Web as dependency.

Step 5:

click on Generate button to download the project files as zip and then extract it on root directory.

Below is the code of pom.xml which took all the project setup and dependency packages info.

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.thedevfamily</groupId>
	<artifactId>usermanagement</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>usermanagement</name>
	<description>User Management</description>

	<properties>
		<java.version>15</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>


Create Greeting.class file:

In this section, we will create a greeting.class controller file that can be exposed as an HTTP Rest API.

package com.thedevfamily.usermanagement.controllers;

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

@RestController
public class Greeting {
    @RequestMapping("/")
    public String greeting() {
        return "Greetings from The Dev Family";
    }
}

We will inject @RestController decorator in Greeting.class. By doing this, we can convert a simple class into REST API which can be used in Spring MVC to handle web requests.

@RequestMapping maps the greeting() with API endpoint ("/") which returns a string as the response of API endpoint.

@RestController combines two annotations one is  @Controller and another one is  @ResponseBody, these two annotations that result in web requests returning data rather than a view.

Create Application.class file:

At the time of project initialization, Spring Initializr creates Application.class that have SpringApplication.run() method to boot up the application. In this class, we will use @SpringBootApplication annotation for running the application.

package com.thedevfamily.usermanagement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UsermanagementApplication {

	public static void main(String[] args) {
		SpringApplication.run(UsermanagementApplication.class, args);
	}

}

Run the Application:

By running the below command in the root directory of project will boot up the application.

./mvnw spring-boot:run
Running the App

In the above screenshot, we will able to see the application is up with localhost and port will be 8080 by default. After launch the app, we will hit the api (http://localhost:8080) from browser or REST API tool like postman.

API call using Postman

Conclusion:

After successfully complete this tutorial, we learned how to set up the project in Spring Boot with the help of Spring Initializr. In this tutorial, we learned how to expose simple API in Spring. In the next tutorial, we will learn How to create CRUD APIs in Spring. If you have any doubt regarding this tutorial or any other doubt, please reach me out to email (developer@thedevfamily.com).

Please refer the code in GitHub. First try yourself, then refer the code.


Resources :


Leave a Reply

Your email address will not be published. Required fields are marked *