第一步:创建项目
1. File->new->project
2. maven->next
3. 写项目名和路径->finish
4.添加pom文件 只需添加
<parent>和 <dependencies>即可,上面的是自动带出来的
<?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>
<groupId>org.example</groupId>
<artifactId>springboot01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
5.写一个主程序和一个controller类 就可以启动项目了
package com.temp.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author: sz
* @Date: Created in
*/
@RestController // 使用@RestController替代@Controller和@ResponseBody
public class Controller {
@RequestMapping("/hello")
public String Hello() {
return "你好,helloWorld!";
}
}
package com.temp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description:
* @Author: sz
* @Date: Created in 2022/3/21
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
// 启动Spring应用
SpringApplication.run(MainApplication.class,args);
}
}
第二步:启动项目
1.启动(run和debug都可以)
2.启动成功后
3.执行http://localhost:8080/hello
备注:
1.添加配置文件
server.port=8081
可以修改端口
也可以用yml文件 ,两者区别可以查看如下快速理解yaml(yml)和properties文件_我要大芒果的博客-CSDN博客_yml和yaml文件区别