You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # spring-boot-demo-swagger
  2. 依赖 [spring-boot-demo-parent](../spring-boot-demo-parent)、[spring-boot-starter-swagger](https://github.com/SpringForAll/spring-boot-starter-swagger) (由大佬[翟永超](http://blog.didispace.com/)开源)
  3. ### pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-swagger</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-swagger</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo-parent</artifactId>
  17. <version>0.0.1-SNAPSHOT</version>
  18. <relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
  19. </parent>
  20. <properties>
  21. <swagger.version>1.5.1.RELEASE</swagger.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>com.spring4all</groupId>
  26. <artifactId>spring-boot-starter-swagger</artifactId>
  27. <version>${swagger.version}</version>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <finalName>spring-boot-demo-swagger</finalName>
  32. </build>
  33. </project>
  34. ```
  35. ### application.yml
  36. ```yaml
  37. server:
  38. port: 8080
  39. context-path: /demo
  40. swagger:
  41. # 是否启用swagger,默认:true
  42. enabled: true
  43. # 标题
  44. title: swagger-demo API 管理
  45. # 描述
  46. description: 这里是 swagger-demo API 管理的描述信息
  47. # 版本
  48. version: 0.0.1-SNAPSHOT
  49. # 许可证
  50. license: MIT License
  51. # 许可证URL
  52. licenseUrl: https://github.com/xkcoding/spring-boot-demo/blob/master/LICENSE
  53. # 许可证URL
  54. termsOfServiceUrl: https://github.com/xkcoding/spring-boot-demo/blob/master/LICENSE
  55. contact:
  56. # 维护人
  57. name: xkcoding
  58. # 维护人URL
  59. url: http://xkcoding.com
  60. # 维护人URL
  61. email: 237497819@qq.com
  62. # swagger扫描的基础包,默认:全扫描
  63. base-package: com.xkcoding
  64. # 需要处理的基础URL规则,默认:/**
  65. base-path: /**
  66. # 需要排除的URL规则,默认:空
  67. exclude-path: /error
  68. # swagger.host=文档的host信息,默认:空
  69. # swagger.globalOperationParameters[0].name=参数名
  70. # swagger.globalOperationParameters[0].description=描述信息
  71. # swagger.globalOperationParameters[0].modelRef=指定参数类型
  72. # swagger.globalOperationParameters[0].parameterType=指定参数存放位置,可选header,query,path,body.form
  73. # swagger.globalOperationParameters[0].required=指定参数是否必传,true,false
  74. ```
  75. ### SpringBootDemoSwaggerApplication.java
  76. ```java
  77. @SpringBootApplication
  78. @EnableSwagger2Doc // 启用 Swagger
  79. public class SpringBootDemoSwaggerApplication {
  80. public static void main(String[] args) {
  81. SpringApplication.run(SpringBootDemoSwaggerApplication.class, args);
  82. }
  83. }
  84. ```
  85. ### User.java
  86. ```java
  87. @Data
  88. @NoArgsConstructor
  89. @AllArgsConstructor
  90. @ApiModel("用户基本信息")
  91. public class User {
  92. private Long id;
  93. @ApiModelProperty("姓名")
  94. @Size(max = 20)
  95. private String name;
  96. @Max(value = 30, message = "年龄小于30岁才可以!")
  97. @Min(value = 18, message = "你必须成年才可以!")
  98. private Integer age;
  99. @Pattern(regexp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", message = "邮箱格式错误!")
  100. private String email;
  101. @Pattern(regexp = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$", message = "请输入正确的手机格式!")
  102. private String phone;
  103. }
  104. ```
  105. ### UserController.java
  106. ```java
  107. @Api(tags = "用户管理")
  108. @RestController
  109. @RequestMapping("/user")
  110. public class UserController {
  111. @ApiOperation("新增用户")
  112. @PostMapping({"", "/"})
  113. public User insert(@RequestBody @Valid User user) {
  114. return user;
  115. }
  116. @ApiIgnore
  117. @DeleteMapping("/{id}")
  118. public String deleteById(@PathVariable Long id) {
  119. return "已删除用户 --> " + id;
  120. }
  121. @ApiOperation("修改用户详情")
  122. @PutMapping("/{id}")
  123. public User update(@PathVariable Long id, @RequestBody @Valid User user) {
  124. user.setId(id);
  125. return user;
  126. }
  127. @ApiOperation("用户详情")
  128. @GetMapping("/{id}")
  129. public User findById(@PathVariable Long id) {
  130. return new User(id, "xkcoding" + id, 21, StrUtil.format("xkcoding{}@163.com", id), StrUtil.fill("186", id.toString().charAt(0), 11, false));
  131. }
  132. @ApiOperation("用户列表")
  133. @GetMapping({"", "/"})
  134. public List<User> index(@ApiParam("第几页") @RequestParam(defaultValue = "1") Integer pageNum, @ApiParam("每页的条目数") @RequestParam(defaultValue = "20") Integer pageSize) {
  135. List<User> users = Lists.newArrayList();
  136. users.add(new User(0L, "xkcoding0", 18, "xkcoding0@163.com", "18600000000"));
  137. users.add(new User(1L, "xkcoding1", 19, "xkcoding1@163.com", "18611111111"));
  138. return users;
  139. }
  140. }
  141. ```
  142. ### 启动项目,查看 API 接口信息
  143. http://localhost:8080/demo/swagger-ui.html
  144. ### 附上大佬(翟永超)博客关于 swagger 的一些文章
  145. http://blog.didispace.com/tags/Swagger/

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。

Contributors (1)