第一个Spring Boot程序

程序使用 idea+maven进行创建
注:还有其它多种创建方法,对于第一个程序,在此先介绍一种
程序结构:
第一个Spring Boot程序

step.1

创建项目:
第一个Spring Boot程序
不用选择模板,直接next,输入项目坐标信息,完成创建

step.2

引入倚赖:

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

其中spring-boot-starter-parent是一个特殊的parent ,其中的倚赖管理可以使引入其它倚赖时不用输入版本号

step.3

编写启动类:

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

该类一般为自动创建

setp.4

创建spring中的控制器:

package org.test;

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";
    }
}

step.5

在此启动方法为:运行App启动类
第一个Spring Boot程序
显示出banner并启动完成后,浏览器输入localhost:8080/hello,结果如下:
第一个Spring Boot程序