在pom.xml中添加依赖

  • 原始swagger-ui相关依赖 ```xml

    io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
  1. - swagger优化UI界面
  2. ```xml
  3. <!-- 2、swagger优化UI界面1 -->
  4. <!-- 请求地址为:http://localhost:8080/doc.html -->
  5. <dependency>
  6. <groupId>com.github.xiaoymin</groupId>
  7. <artifactId>swagger-bootstrap-ui</artifactId>
  8. <version>1.9.6</version>
  9. </dependency>
  10. <!-- 3、swagger优化UI界面2 -->
  11. <!--请求地址为:http://localhost:8080/document.html -->
  12. <dependency>
  13. <groupId>com.zyplayer</groupId>
  14. <artifactId>swagger-mg-ui</artifactId>
  15. <version>1.0.6</version>
  16. </dependency>
  17. <!-- 4、swagger优化UI界面3 -->
  18. <!--请求地址为:http://localhost:8080/docs.html -->
  19. <dependency>
  20. <groupId>com.github.caspar-chen</groupId>
  21. <artifactId>swagger-ui-layer</artifactId>
  22. <version>1.1.3</version>
  23. </dependency>

配置Swagger

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.ApiInfoBuilder;
  4. import springfox.documentation.builders.PathSelectors;
  5. import springfox.documentation.builders.RequestHandlerSelectors;
  6. import springfox.documentation.service.ApiInfo;
  7. import springfox.documentation.service.Contact;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  11. @Configuration
  12. @EnableSwagger2
  13. public class SwaggerConfig {
  14. @Bean
  15. public Docket createRestApi() {
  16. return new Docket(DocumentationType.SWAGGER_2)
  17. .apiInfo(apiInfo())
  18. .select()
  19. //为当前包路径,控制器类包
  20. .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
  21. .paths(PathSelectors.any())
  22. .build();
  23. }
  24. //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
  25. private ApiInfo apiInfo() {
  26. return new ApiInfoBuilder()
  27. // 设置页面标题
  28. .title("Spring Boot集成Swagger2接口总览")
  29. // 设置接口描述
  30. .description("Swagger接口学习")
  31. // 设置联系方式
  32. .contact(new Contact("测试swagger","http://localhost:8080/","123456@qq.com"))
  33. //版本号
  34. .version("1.0")
  35. //描述
  36. .description("API 描述")
  37. .build();
  38. }
  39. }

启动时候报错

guava版本冲突,dubbo与swagger冲突
在pom.xml中引入依赖
https://www.freesion.com/article/969965181/

  1. <dependency>
  2. <groupId>com.google.guava</groupId>
  3. <artifactId>guava</artifactId>
  4. <version>26.0-jre</version>
  5. </dependency>

访问swagger-ui

image.png

image.png

使用swagger2

  • 创建model ```java import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;

@ApiModel(value=”用户实体接受类”) @Data @AllArgsConstructor @NoArgsConstructor public class UserVo { @ApiModelProperty(value=”用户名”) private String name;

  1. @ApiModelProperty(value="密码")
  2. private String pass;

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1416299/1653977696629-01156439-0024-46e4-a77b-614c57a0a7c0.png#clientId=u44488145-64f0-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=207&id=ua21671d6&margin=%5Bobject%20Object%5D&name=image.png&originHeight=207&originWidth=1081&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8781&status=done&style=none&taskId=u40b3751e-3998-48dd-9a80-234583d824a&title=&width=1081)
  2. 1. `**@ApiModel**`:注解用于实体类,表示对类进行说明,用于参数用实体类接收。
  3. 1. `**@ApiModelProperty**`:注解用于类中属性,表示对 model 属性的说明或者数据操作更改。
  4. - 创建controller
  5. ```java
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import io.swagger.annotations.ApiParam;
  9. import org.springframework.web.bind.annotation.*;
  10. @RestController
  11. @Api(value = "swagger接口测试")
  12. public class UserController {
  13. @GetMapping("/test")
  14. @ApiOperation(value = "获取用户信息")
  15. public UserVo getUser(@RequestParam("id") @ApiParam(value = "用户唯一ID") Integer id){
  16. return new UserVo("test","test"+id);
  17. }
  18. @PostMapping("/test")
  19. @ApiOperation(value = "新增用户信息")
  20. public UserVo insertUser(@RequestBody User vo){
  21. return new UserVo(vo.getName(),"success");
  22. }
  23. }

image.png
image.png

  1. **@Api**:注解用于类上,表示这个类是 swagger 的资源。
  2. **@ApiOperation**:注解用于方法,表示一个 http 请求的操作。
  3. **@ApiParam**:注解用于参数上,用来标明参数信息。