1.配置Spring Boot的核心启动器

2.添加starter模块

3.编写测试代码

4.修改Maven默认的App类

5.运行main方法启动Spring Boot应用

6.测试Spring Boot应用
配置Spring Boot的核心启动器

    首先,在pom.xml文件的<url…/>元素之后添加<parent…/>元素配置Spring Boot的核心启动器spring-boot-starter-parent。示例代码如下:
    <parent>        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
添加starter模块

    然后,在pom.xml文件的<dependencies…/>元素中增加一个<dependency…/>元素添加需要的starter模块,此处只添加了spring-boot-starter-web模块。
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
编写测试代码

package com.test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
    @RequestMapping("/hello")
    public String hello() {
        return "您好,Spring Boot!";
    }
}

上述代码中使用的@RestController注解是一个组合注解,相当于Spring MVC中的@Controller和@ResponseBody注解的组合,具体应用如下:
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回JSP或者html页面,返回的内容就是return的内容。
2)如果需要返回到指定页面,则需要用@Controller注解。如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
修改Maven默认的App类

package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Ch3_1Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch3_1Application.class, args);
    }
}


上述代码中使用@SpringBootApplication注解指定该程序是一个Spring Boot应用,该注解也是一个组合注解,相当于@Configuration、@EnableAutoConfiguration和@ComponentScan注解的组合

SpringApplication类调用run方法启动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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.aaa</groupId>
    <artifactId>SpringBootTest02</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootTest02 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version><!--$NO-MVN-MAN-VER$-->
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>SpringBootTest02</finalName>
    </build>
</project>
package com.test;

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

@RestController
public class TestController {
    @RequestMapping("/hello")
    public String hello() {
        return "您好,Spring Boot!";
    }
}
package com.test;

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

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