springboot service怎么实现两个接口
Spring Boot Service可以通过创建多个controller类实现多个接口,每个controller类有一个或多个处理请求的方法,每个方法对应一个接口。以下是一个例子:
- 创建一个controller类并添加@RestController注解
@RestController
public class UserController {
@GetMapping("/user/{id}")
public User getUser(@PathVariable int id) {
// 从数据库中获取指定id的用户信息
User user = userService.getUserById(id);
return user;
}
@PostMapping("/user")
public void addUser(@RequestBody User user) {
// 将用户信息插入到数据库中
userService.addUser(user);
}
}
- 在应用程序类中添加@SpringBootApplication注解
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
- 运行应用程序并访问接口
通过访问http://localhost:8080/user/1可以获取id为1的用户信息,通过访问http://localhost:8080/user并提供用户信息的JSON数据可以将用户信息插入到数据库中
原文地址: https://www.cveoy.top/t/topic/eovj 著作权归作者所有。请勿转载和采集!