2. 创建第一个SpringBoot的项目
2.1. 使用start.spring.io
2.2. sts构建Spring Boot项目
Eclipse + spring插件:
2.3. Intellij IDEA构建Spring Boot项目
2.4. Maven方式构建
|__01-springboot-hello
2.4.1. 创建maven工程
2.4.2. 添加Springboot的依赖
修改pom.xml
添加parent
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修改打包方式
xml
<packaging>jar</packaging>
添加依赖(starter)
xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
完整的pom
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.neuedu</groupId>
<artifactId>spring-boot-mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.4.2.3. 创建IndexController返回json数据
java
package com.neuedu.mvn.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class UserController {
/**
* http://127.0.0.1:8080/index
* @return
*/
@RequestMapping("/index")
Map hello(){
Map map = new HashMap<>();
map.put("success",true);
map.put("msg","成功了");
return map;
}
}
2.4.2.4. 入口程序启动
- 在类上声明@SpringBootApplication注解
- 添加启动的main函数
java
package com.neuedu.mvn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
- 访问: