步骤:
- 添加springboot起步依赖
新建一个Maven项目,选择检查jdk1.8的版本 ,命名
2.创建成功后检查本地仓库是否有东西在下载,在pom.xml中添加
spring-boot-starter-parent org.springframework.boot 2.4.1
org.springframework.boot
spring-boot-starter-web
之后点下刷新,加载依赖。
3.在Java中新建一个类:com.softeem.controller.UserController
4.在UserController中新建一个方法,public String hello
在顶部加@RestController,另外在方法上加 @RequestMapping(“hello”)
package com.softeem.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("hello")
public String hello(){
return "hello world";
}
}
5.在com.softeem包下面新建一个测试类App,在类上面加@SpringBootApplication
package com.softeem;
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);
}
}
6.启动类,在网页上输入http://localhost:8080/hello,就可以在网页上访问到打印输出的语句。
注:项目结构如图



