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

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

第一步

新建一个Spring Initialiar 项目

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

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

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

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

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

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

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

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

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


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

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

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

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

如果你好奇URL为:localhost:8080/test 时,将定位到该TestController处理类上(并未定位到方法),此时如果存在value为空的@GetMapping映射,返回对应方法的返回数据,如果没有找到相关注解则返回404(注意没有@GetMapping注解的方法并不会被调用)
用Spring Boot开发一个web API 接口返回数据


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