1.创建Maven项目看这里 -》 https://blog.youkuaiyun.com/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