XML配置

1
2
3
4
5
6
<!-- Knife Spring Boot 在线API文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>

Controller控制器Api注解配置

类配置 :Api@Api(tags = "xxx")

方法配置 : @ApiOperation("xxx") , @ApiOperationSupport(order = 1~n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Slf4j
//@Lazy
@Api(tags = "3.图片管理模块")
@RestController
@RequestMapping("/album")
public class AlbumController {
public AlbumController() {
log.info("创建相册控制器对象.AlbumController");
}
@Autowired
private IAlbumService albumService;

public AlbumController(IAlbumService albumService) {
log.info("创建相册控制器对象.AlbumController");
}
@ApiOperation("添加相册")
@ApiOperationSupport(order = 10)
@PostMapping("/add-new")
public String addNew(AlbumAddNewDTO albumAddNewDTO){
albumService.addNew(albumAddNewDTO);
}
}

Knife4jConfiguration配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
//测试 http://localhost:9080/doc.html#/home
/**
* 【重要】指定Controller包路径
*/
private String basePackage = "cn.tedu.jsdvn2203.csmall.server.controller";//导入控制器包
/**
* 分组名称
*/
private String groupName = "product";
/**
* 主机名
*/
private String host = "http://java.tedu.cn";
/**
* 标题
*/
private String title = "酷鲨商城在线API文档--商品管理";
/**
* 简介
*/
private String description = "酷鲨商城在线API文档--商品管理";
/**
* 服务条款URL
*/
private String termsOfServiceUrl = "http://www.apache.org/licenses/LICENSE-2.0";
/**
* 联系人
*/
private String contactName = "Java教学研发部";
/**
* 联系网址
*/
private String contactUrl = "http://java.tedu.cn";
/**
* 联系邮箱
*/
private String contactEmail = "java@tedu.cn";
/**
* 版本号
*/
private String version = "1.0.0";

@Autowired
private OpenApiExtensionResolver openApiExtensionResolver;

@Bean
public Docket docket() {
String groupName = "1.0.0";
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.apiInfo(apiInfo())
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build()
.extensions(openApiExtensionResolver.buildExtensions(groupName));
return docket;
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(termsOfServiceUrl)
.contact(new Contact(contactName, contactUrl, contactEmail))
.version(version)
.build();
}

}