1 創建SpringBoot項目
條件:JDK1.8以及以上版本+maven
創建一個maven的java項目,由於我們使用spring-boot-starter-web的啓動器,它內置有Tomcat和SpringMVC所以我們創建一個java程序就可以不需要創建一個web程序。
在pom.xml中添加SpringBoot的依賴
<!-- 添加springboot的父級 -->
<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>
編寫一個web程序
package xin.caoyong.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* Description:編寫一個SpringBoot的HelloWord
* @author CaoYong
* @time 2018年7月9日 下午10:15:18
*/
@RestController
@pringBootApplication
public class IndexController {
/**
*
* Description:
* @author CaoYong
* @time 2018年7月9日 下午10:17:38
* @return
* @return String
*/
@RequestMapping("/index")
public String index() {
return "SprinBoot的第一個項目helloword";
}
public static void main(String[] args) {
SpringApplication.run(IndexController.class, args);
}
}
運行:
第一個紅色框便是啓動成功,第二個紅色框表示Tomcat啓動成功端口號是8080
解釋各個註解的含義
@RestController:表示@Controller(表示Controller層並且添加到Spring容器中)+@ResponseBody(該類中所有方法返回json格式)的集合
@EnableAutoConfiguration :表示自動配置會自動掃描pom.xml文件
SpringApplication.run(IndexController.class, args):啓動項目