10.26

加依赖:在pom.xml里引入spring-boot-starter-web,让项目具备处理 HTTP 请求的能力。
写接口类:用@RestController注解声明这是接口类,再用@GetMapping/@PostMapping指定接口路径。
java
运行
@RestController
public class UserController {
// 前端访问路径:http://localhost:8080/api/user
@GetMapping("/api/user")
public String getUser() {
// 给前端返回数据(实际项目会查数据库,这里简化为字符串)
return "{'name':'张三','age':25}";
}
}
启动服务:运行 Spring Boot 主类,后端服务会在8080端口启动,接口就可以被调用了。

posted @ 2025-10-26 21:10  喜欢写轻小说的日央  阅读(4)  评论(0)    收藏  举报