SwaggerConfiguration.java
2.58 KB
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
package com.trash.garbage.config;
import com.trash.garbage.global.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author guzijian
* 2020/11/07 9:26
* @since:knife4j-spring-boot-fast-demo 1.0
*/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
//添加全局响应状态码
List<ResponseMessage> responseMessageList = new ArrayList<>();
Arrays.stream(ResultCode.values()).forEach(errorEnum -> {
responseMessageList.add(
new ResponseMessageBuilder().code(errorEnum.getCode()).message(errorEnum.getMsg()).responseModel(
new ModelRef(errorEnum.getMsg()))
.build()
);
});
String groupName="学习间swagger";
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.globalResponseMessage(RequestMethod.GET,responseMessageList)
.globalResponseMessage(RequestMethod.POST,responseMessageList)
.globalResponseMessage(RequestMethod.DELETE,responseMessageList)
.globalResponseMessage(RequestMethod.PUT,responseMessageList)
.host("http://localhost:9090/")
.apiInfo(apiInfo())
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.basePackage("com.trash.garbage"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.description(" 学习间接口文档 ")
.contact("204126329@qq.com")
.version("1.0")
.build();
}
}