Spring Boot: Quick Start Tutorial

What is Spring Boot?

Spring Boot is a new framework provided by Pivotal team, which is designed to simplify the development process as well as the initial build new Spring applications. The framework uses a particular manner be configured so that the developer is no longer necessary to define the configuration of the template. In short, Spring Boot default configuration provided by way of integrating all of the framework, so that we can more simply, quickly and easily build applications.

Why use Spring Boot?

Spring Boot contains the following features:

  1. The default mode provides the use of most of the framework to facilitate the rapid integration
  2. Spring Boot application can run independently, in line with the development of the concept of micro-services
  3. Spring Boot built-in WEB container, without having to deploy WAR package to run
  4. It offers a variety of production-ready features, such as indicators, health checks and external configuration
  5. Spring Boot project templates available through the website, initialization convenient project

By these excellent characteristics, Spring Boot can help us very easily and quickly build our project, and can be very convenient for subsequent development, testing and deployment.

The first Spring Boot project

More than just words to practice on. Then, I came to build our first Spring Boot project.

Build the project template

In order for us to initialize the project, Spring Boot provides us with a project template to create website.

1. Open your browser, visit: https://start.spring.io/

The page prompts, select the build tool, language development, project information.

 

3. Click Generate the project, build the project template, will be generated after the archive downloaded to the local.

4. Use the IDE to import project, here I use Eclipse, imported by importing Maven project.

Project Structure Description

As shown below, Spring Boot project structure is relatively simple, containing only three folders.

  • src / main / java program development code placed
  • src / main / resources placed Profiles
  • src / test / java program code disposed Test

In its case, the following main file contains.

  • DemoApplication.java 应用的启动类,包含MAIN方法,是程序的入口
  • application.properties 一个空的配置文件,后续可以配置数据源等信息
  • DemoApplicationTests.java 一个简单的单元测试类
  • pom.xml mave的配置文件,这个应该不用多作介绍了吧

项目结构图

 

添加WEB模块

其实不用添加WEB模块,Spring Boot已经可以启动了。但是为了方便查看,我们引入WEB模块,并添加一个REST接口进行测试。

1.  引入Maven依赖

在 pom.xml中添加web依赖。

<dependency>

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

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

 </dependency>

2.  编写REST接口

新建一个com.louis.springboot.demo.controller包,并创建一个HelloController。

package com.louis.springboot.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }  
}

项目运行测试

1.  右键项目 -> Run as -> Maven install,开始执行Maven构建,第一次会下载Maven依赖,可能需要点时间,如果出现如下信息,就说明项目编译打包成功了。

 

2.  右键文件 DemoApplication.java -> Run as -> Java Application,开始启动应用,当出现如下信息的时候,就说明应用启动成功了,默认启动端口是8080。

 

3.  打开浏览器,访问:http://localhost:8080/hello,返回“Hello Spring Boot!”说明我们添加的REST接口已经测试通过了。

 

代码单元测试

如果要编写单元测试也比较容易,打开的src/test/下的DemoApplicationTests.java文件,我们编写一个测试hello接口的测试方法,对于http请求的测试,我们可以使用mockmvc来模拟,详情参见具体代码。

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    private MockMvc mvc;

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
    }

    @Test
    public void testHello() throws Exception {
       mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }

}

右键DemoApplicationTests.java文件 -> Run as -> Junit Test,运行测试代码,可以从单元测试视图看到单元测试案例运行成功。

 

 

 

开发环境调试

如果每次修改代码之后都需要重新启动WEB应用,还是有点麻烦的,Spring Boot支持热启动,修改之后可以实时生效,开发的时候打开还是可以提供一些便利性的。

打开POM文件,在dependencies标签下添加spring-boot-devtools依赖,并修改build标签下的spring-boot-maven-plugin的fork属性为true即可。

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

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

启动端口修改

Spring Boot默认的启动端口是8080,如果需要修改,需要修改配置文件。

打开application.properties文件,在其中添加如下内容,配置启动端口号。

server.port=8000

重新启动应用,查看控制台启动信息,我们发现启动端口已经变成8000了。

 

Yaml格式文件使目前比较流行的配置文件,我们可以使用Yaml格式配置来代替属性文件配置,将application.properties的文件名修改为application.yml,并将文件内容替换为如下格式内容即可。

server:
  port: 8000

启动Banner定制

我们在应用启动的时候,可以看到控制台显示了Spring的Banner信息,我们可以通过定制这个功能,来放置我们自己的应用信息。

 

如果要定制自己的Banner, 只需要在 resources 下放置一个 baner.txt 文件,输入自己的banner字符即可。

 

Banner字符可以通过类似以下网站生成:

http://patorjk.com/software/taag
http://www.network-science.de/ascii/

 

生成后复制内容到banner.txt并编辑成想要的样式,完成后重启应用,效果如下。

 

源码下载

码云:https://gitee.com/liuge1988/spring-boot-demo.git


作者:朝雨忆轻尘
出处:https://www.cnblogs.com/xifengxiaoma/ 
版权所有,欢迎转载,转载请注明原文作者及出处。

Guess you like

Origin www.cnblogs.com/xifengxiaoma/p/11019240.html
Recommended