SpringMVC整合Springfox-Swagger

关于Swagger的简介就不占篇幅了…

本文使用的Springfox-Swagger版本为2.8.0

spring的版本为4.3.10

jackson版本为2.9.8

1、在ssm的框架上面做操作添加Swagger

2、在Maven中添加pom.xml

1
2
3
4
5
6
7
8
9
10
xml复制代码<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>

选择版本时,最好保持两个包的版本一致,以免出现不可预知的问题~

以上两个是使用Swagger的基本包,如果需要接口自动完成对象和JSON串的转换的话,需要再导入Jackson支持
在整合ssm中我已经添加了jackson-databind架包

3、导入包以后,创建一个简单的Swagger配置类

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
java复制代码package com.zichen.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;

// 仅在没有Spring-boot的项目中需要开启此配置
@EnableWebMvc
// 启用Swagger2
@EnableSwagger2
// 让Spring来加载该类配置
@Configuration
/**
* 也可在Spring配置文件中配置
* <context:component-scan base-package="com.zichen.controller"/>
*/
@ComponentScan(basePackages = "com.zichen.controller")
/**
* @author Sealin
* Created by Sealin on 2018-03-28.
*/
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
//controller匹配规则
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}

private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("开放接口API")
.termsOfServiceUrl("http://localhost:8989/v2/api-docs")
.description("项目名称等描述性词语")
.contact(new Contact("紫晨", "http://www.baidu.net/", "927761@zichen.net"))
.version("1.0")
.build();

}
}

4、要让此配置类生效,需要Spring上下文配置中存在如下选项springmvc-config.xml

1
2
3
4
5
6
7
8
xml复制代码<!--springmvc注解支持-->
<mvc:annotation-driven/>

<!--不拦截静态资源-->
<mvc:default-servlet-handler/>

<!-- 将我们建立的配置类加入Spring容器 -->
<bean class="com.zichen.config.SwaggerConfig" />

swagger官方案例

1
2
3
4
5
6
7
8
xml复制代码<!-- 官方说明 -->
<!-- Required so springfox can access spring's RequestMappingHandlerMapping -->
<mvc:annotation-driven/>

<!-- Required to enable Spring post processing on @Configuration classes. -->
<context:annotation-config/>

<bean class="com.yourapp.configuration.MySwaggerConfig"/>

5、此外,因为我们用Spring实现的Servlet取代了默认的,在处理Swagger-UI的静态资源时,Spring-Servlet并不会帮我们映射这些资源文件,会导致不能访问swagger-ui.html的情况,两种方式可以解决这个问题,任选一种即可。

第一种方案

1
2
xml复制代码<!--不拦截静态资源-->
<mvc:default-servlet-handler />

第二种方案

给Spring-servlet指定我们需要映射的资源文件路径

1
2
xml复制代码    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

上面两种方案可以任意选择一种😀😀😀😀😀😀。

6、到了关键的时刻

至此,Spring和Swagger的整合过程就告一段落了,运行试试:

1
2
3
4
host复制代码API文档视图及操作界面:
http://localhost:8989/swagger/swagger-ui.html
所有API的汇总信息(JSON)
http://localhost:8989/swagger/v2/api-docs

请求路径http://localhost:8989/swagger-ui.html

你们可以试试路径是哪个。不要放弃加油。😀

😀😀😀😀😀😀😀😀

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%