Sample Spring Boot application.

February 01, 2018

Spring Ecosystem has been so popular in “Java World” for the last several years and if you want to be on the cutting-edge of Java technologies you should have Spring Boot in your skills set. I am going to show how to level up your String application with Spring Boot.

Spring Boot logo

I use Spring Boot almost in all my Java Projects. This framework or tool set really makes easy to create Microservices Applications. Here is basic useful features:

  • Embed Tomcat, Jetty or Undertow
  • Automatic configurations
  • Production-ready features and tools.
  • Simple integration with other Spring Projects

You can find more details here.

I created “template project” for quick start. spring-boot-sample. Welcome to fork it and use! In this post I am going to provide short description/explanation on this project.

Requirements

Project structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
├── src
   ├── main
      ├── java
         └── com
             └── alexbezverkhniy
                 └── samples
                     ├── SampleApplication.java
                     ├── controllers
                        └── SampleController.java
                     └── services
                         └── SampleService.java
      └── resources
          └── application.yml
   └── test
       ├── java
          └── com
              └── alexbezverkhniy
                  └── samples
                      ├── controllers
                         └── SampleControllerTest.java
                      └── services
                          └── SampleServiceTest.java
       └── resources
           └── application.yml
├── build.gradle
├── pom.xml
└── README.md

You can see the project has one controller - SampleController.java, one service - SampleService.java and main application itself - SampleApplication.java. You can build project using Maven or Gradle.

Also you can find two tests classes:

  • SampleControllerTest.java - integration test for controller.
  • SampleServiceTest.java - simple test for service.

Build with Gradle

If you going to use Gradle as a build tool. You can see that build.gradle file has only tree dependences.

1
2
3
4
5
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("junit:junit")
}

Everything do you need to build this project just run next command:

1
gradle build

Build with Maven

The same you can see in ‘pom.xml’ file. We have two dependencies.

1
2
3
4
5
6
7
8
9
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>            
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

To build this project run next command:

1
mvn clean install

Services and controllers

Now lets dive deeper into source code. This is very simple project and it has only one controller and one service. We probably can avoid of creating service for such a simple logic, but I am trying to fallow next rule:

Implement all business logic inside services, and use controllers for handling requests and response.

So you can see that we don’t have business logic; in our controller:

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping(value = "/api/sample")
public class SampleController {

    @Autowired
    private SampleService sampleService;

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public String greeting() {
        return sampleService.getGreetingMessage();
    }
}

But we do have it in service

1
2
3
4
5
6
7
8
9
10
@Component
public class SampleService {

    @Value("${name:World}")
    protected String name;

    public String getGreetingMessage() {
        return String.format("Hello %s!", name);
    }
}

Run Spring Boot application

To run Spring Boot application you can use several ways:

Run with gradle

To run application from Gradle you can use Spring Boot Gradle plugin. This plugin includes a run goal which can be used to quickly compile and run your application.

1
gradle bootRun

Run with maven

Similar way for Maven based project, there is Spring Boot Maven plugin. This plugin includes a run goal which can be used to quickly compile and run your application.

1
mvn spring-boot:run

Run as standalone application

Before run the application you need to successfully build it.

1
java -jar target/spring-boot-sample-1.0.0-SNAPSHOT.jar

or in case of Gradle base project:

1
java -jar  build/libs/spring-boot-sample-1.0.0-SNAPSHOT.jar

Testing Application

After running application we can do a little “smoke test” with curl.

1
curl -X GET 'http://localhost:8080/api/sample/greeting'

In the result you should see Hello Alex!.

In my next post I am going to show and explain how to do Integration Testing with Spring Boot.

Written on February 1, 2018
java spring-boot cheat-sheet maven gradle microservices