以下内容都是基于mac + Spring Tool Suite 3 开发工具编写
1.创建项目
填写项目名称与包名
选择spring boot版本,next,直到finished完成
创建完成后,打开pom.xml,进行修改,将以下内容:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
修改为:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
如下图所示:
2.创建一个controoler
(1)新建一个包名:com.exam.demo.controller
(2)创建一个controller: UserTestController类,内容如下:
package com.exam.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/api/") public class UserTestController { @RequestMapping(value = "test", method = RequestMethod.GET) public String getTest() { return "hello world"; } }
3.运行项目
(1)打开SpringbootTestDemoApplication文件,右击Debug As >Spring boot App 运行项目
4.访问controller,在浏览器中执行 http://localhost:8080/api/test 进行接口调用
5.注意事项
要将Application类放在最外侧,即包含所有子包,原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.如下图所示Application类放在包的最外层
至此,一个简单的spring boot接口开发就完成了!!!!