1、添加依赖
1、1
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--swagger3.0 相关依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
1、2 application.yml
spring: devtools: restart: # 设置热部署生效 enabled: true # 设置重启的目录 additional-paths: src/main/java # WEB-INF目录下内容发生更改不重启 exclude: WEB-INF/* server: port: 8080 servlet: context-path: /
2、创建User
package com.houpan.boot_demo02.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * @Author: admin * @Date: 2021/10/27 16:46 * @Description: **/ @ApiModel("用户基本信息") public class User { @ApiModelProperty("姓名") private String name; @ApiModelProperty("年龄") private Integer age; @ApiModelProperty("住址") private String address; @ApiModelProperty("电子邮箱") private String email; public User() { } public User(String name, Integer age, String address, String email) { this.name = name; this.age = age; this.address = address; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
3、创建controller
package com.houpan.boot_demo02.controller; import com.houpan.boot_demo02.entity.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; import java.util.ArrayList; import java.util.List; /** * @Author: admin * @Date: 2021/10/27 16:45 * @Description: **/ @Api(tags = "用户管理") @RestController public class UserController { @ApiOperation("创建用户") @PostMapping("/users") public User create(@RequestBody User user) { return user; } @ApiOperation("用户详情") @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { return new User("bbb", 21, "上海", "aaa@bbb.com"); } @ApiOperation("用户列表") @GetMapping("/users") public List<User> list(@ApiParam("查看第几页") @RequestParam int pageNo, @ApiParam("每页多少条") @RequestParam int pageSize) { List<User> result = new ArrayList<>(); result.add(new User("aaa", 50, "北京", "aaa@ccc.com")); result.add(new User("bbb", 21, "广州", "aaa@ddd.com")); return result; } @ApiOperation("删除用户") @DeleteMapping("/users/{id}") public String deleteById(@PathVariable Long id) { return "delete user : " + id; } }
4、浏览器访问:http://localhost:8080/swagger-ui/index.html