用Spring Boot开发一个web API 接口返回数据

需求:开发微信小程序无法直接获取到服务器数据库数据,所以需要一个API返回
开发软件:IntelliJ IDEA
框架:Spring Boot

第一步

新建一个Spring Initialiar 项目

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

注:SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍


此时我们已经完成一个简单的apiDemo,点击右上角直接启动运行
打开浏览器输入URL为:localhost:8080/test/studentMan 时,将调用注解为@GetMapping(value = “studentMan”)的映射上,返回getStudentManList()方法的返回数据

这里写图片描述

打开浏览器输入URL为:localhost:8080/test/studentWomen 时,将调用注解为@GetMapping(value = “studentWomen”)的映射上,返回getStudentWomenList()方法的返回数据

这里写图片描述

如果你好奇URL为:localhost:8080/test 时,将定位到该TestController处理类上(并未定位到方法),此时如果存在value为空的@GetMapping映射,返回对应方法的返回数据,如果没有找到相关注解则返回404(注意没有@GetMapping注解的方法并不会被调用)
这里写图片描述


小心机:在上面的浏览器测试中我并没有用浏览器,而是使用了一款非常不错的网页调试应用 Postman ,推荐使用

  • 13
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 本身就是一个 Web 框架,可以轻松地将接口封装成 Web API。以下是一些步骤: 1. 定义 Controller 类:在 Spring Boot 中,使用 `@RestController` 注解来定义一个 Controller 类。 ```java @RestController public class MyController { @GetMapping("/hello") public String hello() { return "Hello, world!"; } } ``` 2. 定义 API 接口:在 Controller 类中定义 API 接口,使用 `@GetMapping`、`@PostMapping`、`@PutMapping`、`@DeleteMapping` 等注解来标识不同的 HTTP 请求方法。 ```java @RestController public class MyController { @GetMapping("/hello") public String hello() { return "Hello, world!"; } @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // 根据 id 查询用户信息 } @PostMapping("/user") public User addUser(@RequestBody User user) { // 新增用户信息 } @PutMapping("/user/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { // 根据 id 更新用户信息 } @DeleteMapping("/user/{id}") public void deleteUser(@PathVariable Long id) { // 根据 id 删除用户信息 } } ``` 3. 处理请求参数:在 API 接口中,可以使用 `@RequestParam`、`@PathVariable`、`@RequestBody` 等注解来获取请求参数,并进行相应的处理。 ```java @RestController public class MyController { @GetMapping("/user") public List<User> getUserList(@RequestParam(required = false) String name) { // 根据名称查询用户列表 } @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // 根据 id 查询用户信息 } @PostMapping("/user") public User addUser(@RequestBody User user) { // 新增用户信息 } @PutMapping("/user/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { // 根据 id 更新用户信息 } @DeleteMapping("/user/{id}") public void deleteUser(@PathVariable Long id) { // 根据 id 删除用户信息 } } ``` 4. 返回结果:在 API 接口中,可以直接返回对象、集合等数据类型,Spring Boot 会自动将其转换为 JSON 格式返回给客户端。 ```java @RestController public class MyController { @GetMapping("/user") public List<User> getUserList(@RequestParam(required = false) String name) { // 根据名称查询用户列表 } @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // 根据 id 查询用户信息 } @PostMapping("/user") public User addUser(@RequestBody User user) { // 新增用户信息 } @PutMapping("/user/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { // 根据 id 更新用户信息 } @DeleteMapping("/user/{id}") public void deleteUser(@PathVariable Long id) { // 根据 id 删除用户信息 } } ``` 以上就是将接口封装成 Web API 的基本步骤,当然还可以使用其他的注解和技巧来优化代码和提高性能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值