SpringMvc集成Swagger,搬砖搬来的心得

1.下载Swagger UI

https://github.com/swagger-api/swagger-ui/tree/master/dist

我们需要的是dist中的部分,将/dist下所有的文件复制到项目中。

2.引入jar包(jar包可能会有无用的)

 1 <!--Swagger start-->   
 2         <dependency>
 3             <groupId>io.springfox</groupId>
 4             <artifactId>springfox-swagger2</artifactId>
 5             <version>2.4.0</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>io.springfox</groupId>
 9             <artifactId>springfox-swagger-ui</artifactId>
10             <version>2.4.0</version>
11         </dependency>
12         
13         <dependency>  
14             <groupId>com.fasterxml.jackson.core</groupId>  
15             <artifactId>jackson-core</artifactId>  
16             <version>2.8.0</version>  
17         </dependency>  
18         <dependency>  
19             <groupId>com.fasterxml.jackson.core</groupId>  
20             <artifactId>jackson-databind</artifactId>  
21             <version>2.6.3</version>  
22         </dependency>  
23         <dependency>  
24             <groupId>com.fasterxml.jackson.core</groupId>  
25             <artifactId>jackson-annotations</artifactId>  
26             <version>2.6.3</version>  
27         </dependency>
28 <!--swagger end-->        

3.新增SwaggerConfig.java配置文件

 1    
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 9 import org.springframework.web.util.UriComponentsBuilder;
10 
11 import springfox.documentation.builders.ParameterBuilder;
12 import springfox.documentation.builders.PathSelectors;
13 import springfox.documentation.builders.RequestHandlerSelectors;
14 import springfox.documentation.schema.ModelRef;
15 import springfox.documentation.service.ApiInfo;
16 import springfox.documentation.service.Contact;
17 import springfox.documentation.service.Parameter;
18 import springfox.documentation.spi.DocumentationType;
19 import springfox.documentation.spring.web.paths.AbstractPathProvider;
20 import springfox.documentation.spring.web.paths.Paths;
21 import springfox.documentation.spring.web.plugins.Docket;
22 import springfox.documentation.swagger2.annotations.EnableSwagger2;
23  
24 @Configuration
25 @EnableSwagger2
26 @EnableWebMvc
27 public class SwaggerConfig extends WebMvcConfigurationSupport {
28     @Bean
29     public Docket createAPI() {
30         return new Docket(DocumentationType.SWAGGER_2)
31                 .enable(true)
32                 .forCodeGeneration(true)
33                 //重定义接口路径
34                 .pathProvider(new CustRelativePathProvider()) 
35                 .select()
36                 //限制读取包
37                 .apis(RequestHandlerSelectors.basePackage("com.cqjgsoft.base.controller"))
38 //                过滤生成链接
39                 .paths(PathSelectors.any())
40                 .build()
41                 //为每个方法添加token
42                 .globalOperationParameters(setHeaderToken())
43                 //项目上下文,如果设置错误会导致url如法访问
44                 .pathMapping("/ql")
45                 .apiInfo(apiInfo());
46     }
47 
48     private ApiInfo apiInfo() {
49         Contact contact = new Contact("cqjg", "http://www.jgzxsoft.com/", "1075136624@qq.com");
50         return new ApiInfo("API接口",//大标题 title
51                 "Swagger",//小标题
52                 "0.0.1",//版本
53                 "http://www.jgzxsoft.com/",//termsOfServiceUrl
54                 contact,//作者
55                 "Blog",//链接显示文字
56                 "http://www.jgzxsoft.com/"//网站链接
57         );
58     }
59     
60             public class CustRelativePathProvider extends AbstractPathProvider {
61                 public static final String ROOT = "/";
62          
63                 @Override
64                 public String getOperationPath(String operationPath) {
65                     UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
66                     String uri = Paths.removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
67                     return uri + ".do";
68                 }
69          
70                 @Override
71                 protected String applicationPath() {
72                     return ROOT;
73                 }
74          
75                 @Override
76                 protected String getDocumentationPath() {
77                     return ROOT;
78                 }
79            
80         }
81             /**
82              * 设置请求头
83              * @return
84              */
85             private List<Parameter> setHeaderToken() {
86                 ParameterBuilder tokenPar = new ParameterBuilder();
87                 List<Parameter> pars = new ArrayList<>();
88                 tokenPar.name("X-Auth-Token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
89                 pars.add(tokenPar.build());
90                 return pars;
91             }
92     }

4.修改spring-mvc.xml

加入:   

<bean class="com.xxxx.xxx.SwaggerConfig" />

5.修改index.html中的访问地址

URL需要改为:http://ip:port/projectname/v2/api-docs,后面这部分是不变的,如果你的项目有后缀如:do/d之类的,需要在SwaggerConfig中配置后再加上

 1     $(function(){
 2         // Begin Swagger UI call region
 3         const ui = SwaggerUIBundle({
 4             //url: "http://localhost/ql/swagger-resources/configuration/ui",
 5           url: requestURL+"/v2/api-docs.do",
 6           dom_id: '#swagger-ui',
 7           deepLinking: true,
 8           presets: [
 9             SwaggerUIBundle.presets.apis,
10             SwaggerUIStandalonePreset
11           ],
12           plugins: [
13             SwaggerUIBundle.plugins.DownloadUrl
14           ],
15           layout: "StandaloneLayout"
16         })
17         window.ui=ui;
18     })

 

6.效果图

 

 

其他:

@ApiIgnore注解可以屏蔽类或者方法不被swagger扫描。

 

posted @ 2020-05-20 16:13  受伤的芒果  阅读(443)  评论(0)    收藏  举报