<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>3.0.0version>
dependency>
springdoc:
swagger-ui:
enabled: true
operationsSorter: alpha
tags-sorter: alpha
api-docs:
enabled: true
@Configuration
public class SwaggerConfig {
/** 接口信息 */
@Bean
public OpenAPI apiInfo() {
License license = new License().name("Apache 2.0").url("https://www.wego.com");
Info info =
new Info()
.title("微购商城API文档")
.version("2.0.0")
.description("微型购物商城")
.license(license);
SecurityScheme securityScheme =
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT");
Components components =
new Components().addSecuritySchemes("Authorization", securityScheme);
return new OpenAPI().components(components).info(info);
}
/**
* 接口分组-系统接口
*
* @return
*/
@Bean
public GroupedOpenApi groupedOpenApi() {
String paths[] = {"/**"};
String packagesToScan[] = {"com.wego.controller"};
return GroupedOpenApi.builder()
.group("系统接口")
.packagesToScan(packagesToScan)
.pathsToMatch(paths)
.build();
}
}
```java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel(value = "Dept",description = "部门信息")
public class Dept implements Serializable {
private static final long serialVersionUID = -4383838089518165322L;
/**
* 部门编号
*/
@ApiModelProperty("部门编号")
private Integer deptno;
/**
* 部门名称
*/
@ApiModelProperty("部门名称")
private String dname;
/**
* 部门地址
*/
@ApiModelProperty("部门地址")
private String loc;
}
### 第四步:测试Controller
```java
@Api(tags = "用户信息管理", value = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
@ApiResponse(message = "测试", code = 200)
@ApiOperation(value = "fun", notes = "测试方法fun", httpMethod = "GET")
@GetMapping("/fun")
public String fun() {
return "fun";
}
@PutMapping(value = "/v1/{dname}")
@ApiOperation(value = "修改部门信息", notes = "", httpMethod = "PUT")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(paramType = "query", name = "deptno", value = "部门ID", required = true, dataType = "Integer", dataTypeClass = Integer.class),
@ApiImplicitParam(paramType = "path", name = "dname", value = "部门名称", required = true, dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(paramType = "body", name = "dept", value = "用户实体", required = true, dataType = "Dept", dataTypeClass = Dept.class)
})
public String fun(@RequestParam(name = "deptno", value = "deptno", required = false) Integer deptno,
@PathVariable(value = "dname", required = true) String dname,
@RequestBody(required = true) Dept dept,
HttpServletRequest request) {
String token = request.getHeader("token");
return token + " " + deptno + " " + dname + " " + dept;
}
}
访问页面http://localhost/is3/swagger-ui/index.html,效果:
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>knife4j-spring-boot-starterartifactId>
<version>3.0.3version>
dependency>
#配置swagger配置
knife4j:
basic:
username: admin
password: 666666
enable: true #开启认证
production: false #默认是false ,屏蔽所有Swagger的相关资源
enable: true #是否开启swagger
访问网页:http://localhost/is3/doc.html#/home效果:
备注:swagger和knife两种技术可以共同使用,也可以只使用其中的任何一种。