在pom.xml中添加依赖
原始swagger-ui相关依赖 ```xml
io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
- swagger优化UI界面
```xml
<!-- 2、swagger优化UI界面1 -->
<!-- 请求地址为:http://localhost:8080/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!-- 3、swagger优化UI界面2 -->
<!--请求地址为:http://localhost:8080/document.html -->
<dependency>
<groupId>com.zyplayer</groupId>
<artifactId>swagger-mg-ui</artifactId>
<version>1.0.6</version>
</dependency>
<!-- 4、swagger优化UI界面3 -->
<!--请求地址为:http://localhost:8080/docs.html -->
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>1.1.3</version>
</dependency>
配置Swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径,控制器类包
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("Spring Boot集成Swagger2接口总览")
// 设置接口描述
.description("Swagger接口学习")
// 设置联系方式
.contact(new Contact("测试swagger","http://localhost:8080/","123456@qq.com"))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
启动时候报错
guava版本冲突,dubbo与swagger冲突
在pom.xml中引入依赖
https://www.freesion.com/article/969965181/
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
访问swagger-ui
使用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;
@ApiModelProperty(value="密码")
private String pass;
}
![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)
1. `**@ApiModel**`:注解用于实体类,表示对类进行说明,用于参数用实体类接收。
1. `**@ApiModelProperty**`:注解用于类中属性,表示对 model 属性的说明或者数据操作更改。
- 创建controller
```java
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(value = "swagger接口测试")
public class UserController {
@GetMapping("/test")
@ApiOperation(value = "获取用户信息")
public UserVo getUser(@RequestParam("id") @ApiParam(value = "用户唯一ID") Integer id){
return new UserVo("test","test"+id);
}
@PostMapping("/test")
@ApiOperation(value = "新增用户信息")
public UserVo insertUser(@RequestBody User vo){
return new UserVo(vo.getName(),"success");
}
}
**@Api**
:注解用于类上,表示这个类是 swagger 的资源。**@ApiOperation**
:注解用于方法,表示一个 http 请求的操作。**@ApiParam**
:注解用于参数上,用来标明参数信息。