入门步骤: 一:创建maven工程 二:添加起步依赖 三:编写引导类 四:编写Controller
SpringBoot要求,所有springboot项目都要继承SpringBoot的起步依赖spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
我们案例后面SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//标明该类是SpringBoot的引导类
public class MySpringBootApplication {
public static void main(String[] args){
SpringApplication.run(MySpringBootApplication.class);
}
}
package com.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickController {
@RequestMapping("/quick")
@ResponseBody
public String showMessage(){
return "哈哈哈哈哈 成功了";
}
}
SpringBoot启动时候默认启动一个tomcat 默认路径http://localhost:8080
我们在Controller中定义了一个Controller 其中一个方法映射路径为/quick则
我们用http://localhost:8080/quick即可访问到项目
main只是程序入库,并不一定是引导类 谁标明了@SpringBootApplication谁是引导类 SpringApplication.run(MySpringBootApplication.class);中 SpringApplication.run()代表启动引导类,参数为引导类的字节码class 但是我们为了方便一般把引导类直接写到main方法中,只起启动引导类作用