How to Create a Spring Boot application with Intellij IDEA

Spring Boot is a framework which we can use to create standalone, production grade enterprise level applications which we can just run easily.

In this tutorial lets see how we can create our first Spring Boot Application.

First go to File -> Project -> Select Maven

Click Next

Click Next -> Finish

Open the pom.xml file and change it as below

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>article</groupId>
   <artifactId>article</artifactId>
   <version>1.0-SNAPSHOT</version>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.RELEASE</version>
   </parent>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
   </dependencies>
</project>

Create a Package name main under the java folder

Create a Class name MainController.java and change it as below.
@SpringBootApplication indicate the Starting point of the Spring boot application

SpringApplication.run() will perform task like Sets up default configurations, Starts Spring application context, Perform class path scan, Start Tomcat Server

package main;

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

@SpringBootApplication
public class MainController {

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

Create a another Class name Article.java and change it as below. This class we use to keep as an array of data objects. 

 package main;

public class Article {

    private String id;
    private String name;
    private String description;

    public Article() {
    }

    public Article(String id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Create a Class name ArticleService.java and change it as below

@Service – : A Singleton Service, When application startup, spring creates an instance of the service and keep it in the memory. we can have other services or classes which depend on a service. 

package main;

import model.Article;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ArticleService {

    List<Article> articleList = new ArrayList<Article>(Arrays.asList(
            new Article("1","Article 01", "Description 01"),
            new Article("2","Article 02", "Description 02"),
            new Article("3","Article 03", "Description 03")
    ));

    public List<Article> getAllArticles(){
        return articleList;
    }

    public Article getArticle(String id)
    {
        return articleList.stream().filter(t -> t.getId().equals(id)).findFirst().get();
    }

    public void addArticle(Article article) {

        articleList.add(article);
    }

    public void updateArticle(Article art,String id) {

        for(int i = 0; i< articleList.size(); i++)
        {
            Article article = articleList.get(i);

            if(article.getId().equals(id))
            {
                articleList.set(i,art);
            }

        }
    }

    public void deleteArticle(String id) {
        articleList.removeIf(t->t.getId().equals(id));
    }

}

Create a Class name ArticleController.java and change it as below.

package main;

import model.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/articles")
    public List<Article> getAllArticles()
    {
       return articleService.getAllArticles();
    }

    @RequestMapping("/articles/{id}")
    public Article getArticle(@PathVariable String id){
        return articleService.getArticle(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/articles")
    public void addArticle(@RequestBody Article article)
    {
        articleService.addArticle(article);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/articles/{id}")
    public void updateArticle(@RequestBody Article article, @PathVariable String id)
    {
        articleService.updateArticle(article,id);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/articles/{id}")
    public void deleteArticle(@PathVariable String id)
    {
        articleService.deleteArticle(id);
    }


}

Finally run the MainController.java file

GET

POST Article 

PUT Article

DELETE Article

Leave a Reply

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