Spring Boot – Setting up your first project

Nowadays Spring boot is a framework utilized for many important projects developed using the JAVA programming language. Then know how to use spring boot is a very important skill for any software developer.

In this article we are going to see how to setup a spring boot project from zero. Then only requirement is that you have java and an eclipse IDE installed with updated version.

First, create a new Maven Project:  From the Upper menu, File>New>Project



Then, you could chose  to create a simple project:



Next step set project group id and the project artifact id:


Now the project is created. The next step is to update the pom.xml with the dependencies your project needs.

Here is an example of the minimum pom.xml file you have to create. You could just copy and paste this code to you project. This pom.xml just has the following: spring boot core, spring boot for web application, spring JPA, and spring database implementation. We also have a parent tag, it is useful to not have se the versions individually for each dependency. It is important to highlight we set the java version for as 1.8, as it is compatible with spring boot.

<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>springboot.demo</groupId>

    <artifactId>springboot_app</artifactId>

  <version>0.0.1-SNAPSHOT</version>

     <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.2.5.RELEASE</version>

    </parent>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

    </properties>

    <dependencies>

        <!-- Core -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <!-- Web application -->

       <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <!-- Spring Data JPA -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>

        <!-- Embedded DB: H2 -->

        <dependency>

            <groupId>com.h2database</groupId>

            <artifactId>h2</artifactId>

            <scope>runtime</scope>

        </dependency>

    </dependencies>

 </project>

Now the pom.xml is ready you may download the dependencies running maven install. You may find that with right click on you project>Run As>Maven Install



Now you have all dependencies, next step is to create the main method, that will start you JAVA APP, but we have to annotate that with spring boot annotations for it understands it has to run as a spring boot application. Basically you just have to annotate you class with @SpringBootApplication, and within the main method call the SpringApplication.run method. You may see that in the code below:

 import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 @RestController

@SpringBootApplication

public class SpringBootWebApp {

 

       @RequestMapping("/")

       String home() {

             return "Hello World - Spring boot project 1.0!";

       }

             public static void main(String[] args) {

             SpringApplication.run(SpringBootWebApp.class, args);

       }

}

Now, aiming to test the code, we have annotated the class with @RestController, it will allows some methods to be accessed based on HTTP Request. Then the method home was annotated with @RequestMapping("/"), indicating When application runs and is accessed in its root path, the output of this method will de displayed.

 

The last step for running the application is set the goals value for Maven build. It has to be set as: spring-boot:run. For setting that you may right click your project>run As>Run Configurations.



The it is ready to be executed. Now just right click you project>Run As> Maven Build. You should have this output.

 


This code is entirely available from github:

https://github.com/rafaelqg/springboot_example

You may see a video class based in the video below:



Comments

Popular posts from this blog

HASHLIB: Using HASH functions MD5 and SHA256

Spread Operator

Dart: implementing Object Oriented Programming (OOP)