SpringBoot整合Swagge3案例

x33g5p2x  于2022-02-21 转载在 Spring  
字(5.1k)|赞(0)|评价(0)|浏览(473)

1.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yl</groupId>
  12. <artifactId>swagger3</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>swagger3</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <!--swagger依赖-->
  25. <dependency>
  26. <groupId>io.springfox</groupId>
  27. <artifactId>springfox-boot-starter</artifactId>
  28. <version>3.0.0</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

2.user类

  1. package com.yl.swagger3.model;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import java.io.Serializable;
  5. @ApiModel(value = "用户实体类",description = "一个定义用户信息的实体类")
  6. public class User implements Serializable {
  7. @ApiModelProperty("用户id")
  8. private Integer id;
  9. @ApiModelProperty("用户名")
  10. private String username;
  11. @ApiModelProperty("密码")
  12. private String password;
  13. public Integer getId() {
  14. return id;
  15. }
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19. public String getUsername() {
  20. return username;
  21. }
  22. public void setUsername(String username) {
  23. this.username = username;
  24. }
  25. public String getPassword() {
  26. return password;
  27. }
  28. public void setPassword(String password) {
  29. this.password = password;
  30. }
  31. @Override
  32. public String toString() {
  33. return "User{" +
  34. "id=" + id +
  35. ", username='" + username + '\'' +
  36. ", password='" + password + '\'' +
  37. '}';
  38. }
  39. }

3.swagger配置

  1. package com.yl.swagger3.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.Contact;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. // swagger的配置
  11. @Configuration
  12. public class SwaggerConfig {
  13. @Bean
  14. Docket docket() {
  15. return new Docket(DocumentationType.OAS_30)
  16. .select()
  17. .apis(RequestHandlerSelectors.basePackage("com.yl.swagger3.controller"))
  18. .paths(PathSelectors.any())
  19. .build()
  20. .apiInfo(
  21. new ApiInfoBuilder()
  22. .description("xxx项目接口文档")
  23. .contact(new Contact("root","http://www.baidu.com","xxx@qq.com"))
  24. .version("V 1.0")
  25. .title("Api测试文档")
  26. .license("Apache2.0")
  27. .licenseUrl("http://www.baidu.com")
  28. .build()
  29. );
  30. }
  31. }

4.controller

  1. package com.yl.swagger3.controller;
  2. import com.yl.swagger3.model.User;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import io.swagger.annotations.ApiOperation;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.Parameter;
  8. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  9. import io.swagger.v3.oas.annotations.responses.ApiResponses;
  10. import org.springframework.web.bind.annotation.*;
  11. import springfox.documentation.annotations.ApiIgnore;
  12. @RestController
  13. public class UserController {
  14. @GetMapping("/hello")
  15. public String hello() {
  16. return "hello swagger3";
  17. }
  18. @GetMapping("/getUserById/{id}")
  19. @ApiOperation(value = "查询用户",notes = "根据id查询用户") //接口详情
  20. @Operation(summary = "查询用户",description = "根据id查询用户")
  21. @ApiImplicitParams({
  22. @ApiImplicitParam(paramType = "path",name = "id",required = true)
  23. }) //参数
  24. @ApiResponses({
  25. @ApiResponse(responseCode = "200", description = "请求成功"),
  26. @ApiResponse(responseCode = "500", description = "请求出错")
  27. } // 响应码描述
  28. )
  29. public String getUserById(@PathVariable Integer id) {
  30. return "user1";
  31. }
  32. @ApiIgnore //忽略生成接口文档
  33. @GetMapping("hello1")
  34. public void hello1() {
  35. System.out.println("hello1");
  36. }
  37. @PostMapping("/addUser")
  38. @Parameter(name = "user",required = true) //参数
  39. public String addUser(@RequestBody User user) {
  40. return user.toString();
  41. }
  42. }

5.swagger-ui访问地址,http://localhost:8080/swagger-ui/index.html

6.api接口地址,http://localhost:8080/v3/api-docs

相关文章