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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # spring-boot-demo-springdoc
  2. > 此 demo 主要演示了 Spring Boot 如何通过 Springdoc 集成 swagger
  3. ## 1.开发步骤
  4. ### 1.1.添加依赖
  5. > 3.0.0-M5 依赖的 servlet 为 jakarta,目前 release 的 Springdoc 依赖的是 javax.servlet-api,需要同时使用最新的 2.0.0-M5
  6. ```xml
  7. <dependencies>
  8. <dependency>
  9. <groupId>com.xkcoding</groupId>
  10. <artifactId>common-tools</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springdoc</groupId>
  23. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  24. <version>${springdoc.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.projectlombok</groupId>
  28. <artifactId>lombok</artifactId>
  29. <optional>true</optional>
  30. </dependency>
  31. </dependencies>
  32. ```
  33. ### 1.2.编写测试接口
  34. ```java
  35. @Slf4j
  36. @RestController
  37. @RequestMapping("/user")
  38. @Tag(name = "用户管理", description = "用户管理")
  39. public class UserController {
  40. @GetMapping
  41. @Operation(summary = "条件查询(DONE)", description = "备注")
  42. @Parameters({
  43. @Parameter(name = "username", description = "用户名", in = ParameterIn.QUERY, schema = @Schema(implementation = String.class), required = true)
  44. })
  45. public Response<User> getByUserName(String username) {
  46. log.info("多个参数用 @Parameters");
  47. return Response.ofSuccess(new User(1, username, "JAVA"));
  48. }
  49. @GetMapping("/{id}")
  50. @Operation(summary = "主键查询(DONE)", description = "备注")
  51. @Parameters({
  52. @Parameter(name = "id", description = "用户编号", in = ParameterIn.PATH, schema = @Schema(implementation = Integer.class), required = true)
  53. })
  54. public Response<User> get(@PathVariable Integer id) {
  55. log.info("单个参数用 @Parameter");
  56. return Response.ofSuccess(new User(id, "u1", "p1"));
  57. }
  58. @DeleteMapping("/{id}")
  59. @Operation(summary = "删除用户(DONE)", description = "备注")
  60. @Parameter(name = "id", description = "用户编号", in = ParameterIn.PATH, schema = @Schema(implementation = Integer.class), required = true)
  61. public void delete(@PathVariable Integer id) {
  62. log.info("单个参数用 Parameter");
  63. }
  64. @PostMapping
  65. @Operation(summary = "添加用户(DONE)")
  66. public User post(@RequestBody User user) {
  67. log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @Parameter");
  68. return user;
  69. }
  70. @PostMapping("/multipar")
  71. @Operation(summary = "添加用户(DONE)")
  72. public List<User> multipar(@RequestBody List<User> user) {
  73. log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @Parameter");
  74. return user;
  75. }
  76. @PostMapping("/array")
  77. @Operation(summary = "添加用户(DONE)")
  78. public User[] array(@RequestBody User[] user) {
  79. log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @Parameter");
  80. return user;
  81. }
  82. @PutMapping("/{id}")
  83. @Operation(summary = "修改用户(DONE)")
  84. public void put(@PathVariable Long id, @RequestBody User user) {
  85. log.info("如果你不想写 @Parameter 那么 swagger 也会使用默认的参数名作为描述信息 ");
  86. }
  87. @PostMapping("/{id}/file")
  88. @Operation(summary = "文件上传(DONE)")
  89. public String file(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
  90. log.info(file.getContentType());
  91. log.info(file.getName());
  92. log.info(file.getOriginalFilename());
  93. return file.getOriginalFilename();
  94. }
  95. }
  96. ```
  97. ### 1.3.编写返回对象
  98. ```java
  99. @Data
  100. @NoArgsConstructor
  101. @AllArgsConstructor
  102. @Schema(name = "User", title = "用户实体", description = "User Entity")
  103. public class User implements Serializable {
  104. @Serial
  105. private static final long serialVersionUID = 5057954049311281252L;
  106. /**
  107. * 主键id
  108. */
  109. @Schema(title = "主键id", required = true)
  110. private Integer id;
  111. /**
  112. * 用户名
  113. */
  114. @Schema(title = "用户名", required = true)
  115. private String name;
  116. /**
  117. * 工作岗位
  118. */
  119. @Schema(title = "工作岗位", required = true)
  120. private String job;
  121. }
  122. ```
  123. ### 1.4.配置 Springdoc
  124. - **SpringdocAutoConfiguration**
  125. ```java
  126. @Configuration(proxyBeanMethods = false)
  127. @OpenAPIDefinition(info = @Info(title = "spring-boot-demo-apidoc-swagger", version = "1.0.0-SNAPSHOT", description = "这是一个简单的 Swagger API 演示", contact = @Contact(name = "Yangkai.Shen", url = "https://xkcoding.com", email = "237497819@qq.com")),
  128. externalDocs = @ExternalDocumentation(description = "springdoc官方文档", url = "https://springdoc.org/"),
  129. servers = @Server(url = "http://localhost:8080/demo")
  130. )
  131. public class SpringdocAutoConfiguration implements WebMvcConfigurer {
  132. }
  133. ```
  134. - **application.yml**
  135. ```yaml
  136. server:
  137. port: 8080
  138. servlet:
  139. context-path: /demo
  140. springdoc:
  141. swagger-ui:
  142. path: /swagger-ui.html
  143. packages-to-scan: com.xkcoding.swagger.controller
  144. ```
  145. ## 2.测试
  146. 启动项目,访问地址:http://localhost:8080/demo/swagger-ui/index.html
  147. ## 3.参考
  148. - [Springdoc 官方文档](https://springdoc.org/)
  149. - [Springfox 迁移到 Springdoc 步骤](https://springdoc.org/#migrating-from-springfox)