快速搭建springboot下的restful接口

快速搭建springboot下的restful接口

环境 版本
系统: Windows10
Java: JDK11
Maven: 3.63
开发工具: idea 2019.3.4

1.搭建一个springboot空应用

  • 打开idea – Create New Project
    快速搭建springboot下的restful接口
  • 左边导航栏选择 – Spring Initializr, 选择JDK版本后点next
    快速搭建springboot下的restful接口
  • 给项目确立一个名字(project metadata)
    group: 一般以公司域名倒序 例如:www.baidu.com 这里就填:com.baidu
    artifact: 这里填写你的项目名字
    type: 选择使用 Maven Project
    language: 用java语言开发
    packaging: jar (springboot 都是以jar包形式运行)
    java Version: 使用哪个版本的Java 编译这个项目,这里我们使用11
    快速搭建springboot下的restful接口
  • 选择需要集成的组件
    这一步比较关键,因为我们是要创建一个restful的接口,resful是基于http的,那么显然这里我们要用到spring 的web组件。右上角可以选择springboot的版本
    快速搭建springboot下的restful接口
  • 给项目起一个名字(一般和artifact起同样的名字)
    快速搭建springboot下的restful接口
  • 这样一个spring空应用就搭建好了,可以这右上角直接点击运行把项目启动起来
    快速搭建springboot下的restful接口
    可以看console里面,现在spring就起来了
    快速搭建springboot下的restful接口

2.编写restful接口

  • 这里我先添加一个Controller文件夹
    快速搭建springboot下的restful接口
    快速搭建springboot下的restful接口
  • 在Controller文件夹下添加TestController.java文件
    快速搭建springboot下的restful接口
    快速搭建springboot下的restful接口
    快速搭建springboot下的restful接口
  • 添加@RestController(等同于@Controller + @ResponseBody) 以及 @RequestMapping ,指定请求为Get请求
    快速搭建springboot下的restful接口
    这里的requestMapping 加上去后,相当于给了一个http的url路径
    例如百度 : www.baidu.com /test/getMethod 拼在网址后面
  • 启动项目,测试接口
    快速搭建springboot下的restful接口
    在浏览器里输入 http://127.0.0.1:8080/test/getMethod 就能访问到我们的接口了。