Swagger2
参考文档:https://www.jianshu.com/p/5ae7267385b9
注解参考:https://blog.csdn.net/weixin_41846320/article/details/82970204
一、Swagger2 是什么?
一个规范和完整的框架,用于生成、描述、调用和可视化RESTfil 风格的Web,让RESTFUL 风格的API 可视化,方便管理,同事提供测试功能测试每个RESTFUL API
二、如何在Spring boot 部署
1、添加依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2、生成 Swagger类
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInf()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInf() { return new ApiInfoBuilder() .title("系统RESTful API文档") .contact(new Contact("mrbird", "https://mrbird.cc", "852252810@qq.com")) .version("1.0") .build(); } }
3、在Controller 层打 Swagger2 标签
@Api(value = "用户Controller") @Controller @RequestMapping("user") public class UserController { @ApiIgnore @GetMapping("hello") public @ResponseBody String hello() { return "hello"; } @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息") @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path") @GetMapping("/{id}") public @ResponseBody User getUserById(@PathVariable(value = "id") Long id) { User user = new User(); user.setId(id); user.setName("mrbird"); user.setAge(25); return user; } @ApiOperation(value = "获取用户列表", notes = "获取用户列表") @GetMapping("/list") public @ResponseBody List<User> getUserList() { List<User> list = new ArrayList<>(); User user1 = new User(); user1.setId(1l); user1.setName("mrbird"); user1.setAge(25); list.add(user1); User user2 = new User(); user2.setId(2l); user2.setName("scott"); user2.setAge(29); list.add(user2); return list; } @ApiOperation(value = "新增用户", notes = "根据用户实体创建用户") @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") @PostMapping("/add") public @ResponseBody Map<String, Object> addUser(@RequestBody User user) { Map<String, Object> map = new HashMap<>(); map.put("result", "success"); return map; } @ApiOperation(value = "删除用户", notes = "根据用户id删除用户") @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path") @DeleteMapping("/{id}") public @ResponseBody Map<String, Object> deleteUser(@PathVariable(value = "id") Long id) { Map<String, Object> map = new HashMap<>(); map.put("result", "success"); return map; } @ApiOperation(value = "更新用户", notes = "根据用户id更新用户") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path"), @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") }) @PutMapping("/{id}") public @ResponseBody Map<String, Object> updateUser(@PathVariable(value = "id") Long id, @RequestBody User user) { Map<String, Object> map = new HashMap<>(); map.put("result", "success"); return map; } }
4.访问:localhost:8080/swagger-ui.html