1.創建Maven項目看這裏 -》 https://blog.csdn.net/qq_37231511/article/details/90231243
2.根據步驟1創建項目結構如圖:
3.編輯pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cx</groupId>
<artifactId>springBootProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springBootProject</name>
<url>http://maven.apache.org</url>
<!-- SpringBoot基本環境 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!-- 編碼 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Web應用基本環境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Maven Jar包環境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<!-- 調試環境 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version><!--$NO-MVN-MAN-VER$-->
<!-- 發佈時不使用此包 -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.創建主程序類MApplication
package com.cx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @ClassName: MApplication.java
* @Description: 該類的功能描述
* @version: v1.0.0
* @author: cx
* @date: 2019年5月24日 上午11:30:32
*/
@SpringBootApplication //標註一個主程序類,表示這是一個SpringBoot應用
public class MApplication {
public static void main(String[] args) {
SpringApplication.run(MApplication.class, args);
}
}
5.創建controller包,在包中創建控制器類SpringController
package com.cx.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: SpringController.java
* @Description: 該類的功能描述
* @version: v1.0.0
* @author: cx
* @date: 2019年5月24日 上午11:35:51
*/
@RestController //Controller+ResponseBody,即整個Controller方法的返回值爲Json或Json對象
public class SpringController {
/**
* 當作用在控制器類上的時候,該註解會應用到控制類中所有的方法上,控制器類中方法使用的RequestMapping都是基於控制器類上的RequestMapping
* 當直接作用在方法上的時候,即絕對路徑,也就是根路徑"/"而言
*/
@RequestMapping("/") //配置url映射到控制器類或控制器類的方法上,作用在控制器類中的方法上或作用在控制器類上
public String index(){
return "This is First SpringBoot Project";
}
}
6.運行主類,在瀏覽器輸入localhost:8080,看到This is First SpringBoot Project,即成功
7.源碼已上傳github,下載網址:https://github.com/snowlavenderlove/SpringBootProject