Spring Boot (1) – Trial

 · 3 mins read

What is Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.

Features

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated ‘starter’ POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

Just put them into maven pom.xml then we can start with our project.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Let’s go

Alternatively, I would go with spring initializer to create a out-of-box project immediately.

It’s great to choose some starter projects, such as WEB, JPA, devtools etc. After downloading the zip file, just import it into IDE, Eclipse or Intellij IDEA which I personally choose.

Then let’s take a look at project structure.

/src/main/java: java sources, including these blow

StudyApplication: gl.obal framework configuration, main entry

controller: the “C” in MVC, request mapping and reponse organization

dao: short for Data Access Object, responsible for data retrieving, storage, reorganization

entity: data entity, the model, “M” in MVC. Some may differentiate model between entity, model could be a combination of entities.

service: implemention of the business logic

util: various of utilities, tools

consts: constants of projects

/src/main/resources:

static: static files, visit directly by url, which means the request will not be delivered into DispatchServelet

templtaes: template files to be rendered by server, freemarker, thymeleaf etc. Spring recommends to use thymeleaf for SSR, so I would try to use it.

There is another code structure that should be mentioned, which is based on components. See this below:

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

Anyway, it’s your choice. The last thing, Write controller, then run.

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}
//if you don't need datasource configuration, just add this or you will get a error
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class StudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(StudyApplication.class, args);
    }
}

RestController annotation will return json so that you don’t have to use jackson.

open browser, visit http://localhost:8080/hello, then you will see Hello World

If you need hot reload, remember to add devtools to dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>runtime</scope>
</dependency>

Edit a java file, save, then it will run! Enjoy!

Here is the official spring boot reference which is very detailed and elaborate.