项目中使用的swagger框架在生成api文档时存在一些问题:
1、 控制器下方法无法点击展开
2、api内容结构混乱

基于上述原因,重新整合重构了一下api文档生成的代码。在此将重整过程记录下来,方便后续查看。

Maven依赖引入
要整合SpringFox所需的依赖如下:

<!--springfox-swagger需要的最小依赖 start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>

<!--jackson用于将springfox返回的文档对象转换成JSON字符串-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${version.jackson}</version>
</dependency>

<!--petStore是官方提供的一个代码参考, 可用于后期写文档时进行参考, 可不加-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-petstore</artifactId>
<version>2.5.0</version>
</dependency>
<!--最小依赖 end-->
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
由于项目框架版本限制,此处只使用2.6版本的SpringFox。另外,根据项目实际情况解决依赖冲突。

添加swagger配置类
import org.springframework.context.annotation.Bean;
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.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* @author yanzy
* @description api文档配置类
* @date 2018-03-27 14:55
* @created by intelliJ IDEA
*/
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket api() {

return new Docket(DocumentationType.SWAGGER_2)
.groupName("api") // 组名
.tags(new Tag("SMR-Services","xxx控制器"),
new Tag("Shiro-Services","xxx控制器"))
.select() // 选择哪些路径和api会生成document
.apis(RequestHandlerSelectors.basePackage("packageName"))//// 对packageName包中的api进行监控
.paths(PathSelectors.any()) // regex, ant, any (default), none 格式:PathSelectors.any()
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SDD api navigation")//You application title. Required.
.description("API Document")//API description. Arbitrary text in CommonMark or HTML.
.version("1.0.0")//API version. You can use semantic versioning like 1.0.0, or an arbitrary string like 0.99-beta. Required.
.termsOfServiceUrl("no terms of service")//Link to the page that describes the terms of service.Must be in the URL format.
.license("Apache 2.0")//Name of the license.
//.contact(new Contact("yanzy","","yanzy@dist.com.cn"))//Contact information: name, email, URL.
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")//A URL to the license description.
.build();
}

@Bean
UiConfiguration uiConfiguration() {
UiConfiguration uiConfiguration = new UiConfiguration(null);
return uiConfiguration;
}
}
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
此处特别注意tags的使用,上述问题2中无法点击展开控制器下方法就是由于在控制器的@Api标签中使用tags参数导致的。

SpringMVC的XML配置文件相关配置
<!-- Swagger config配置类的路径,注意放在此处,否则生成不了api doc -->
<bean class="dist.dgp.configs.SwaggerConfig"/>
<!--下面两个是静态资源访问配置-->
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
1
2
3
4
具体使用用例
import dist.dgp.bll.impls.smr.SMRMaterialServiceImpl;
import dist.dgp.model.smr.pos.MaterialPo;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* @author yanzy
* @description
* @date 2018-04-11 14:42
* @created by intelliJ IDEA
*/
@RestController
@RequestMapping("materialController")
@Api
@Data
@Slf4j
public class MaterialService {
@Autowired
SMRMaterialServiceImpl smrMaterialService;

@RequestMapping(value = "/create",method = {RequestMethod.POST})
@ApiOperation(tags = {"SMR-Services"},value = "Material-新建")
@ApiImplicitParams({
@ApiImplicitParam(name = "materialPo", value = "资料报告对象", required = true, paramType = "RequestBody", dataType = "MaterialPo")
})
@ApiResponses({
@ApiResponse(code = 200,message = "OK",response = boolean.class),
@ApiResponse(code = 400,message = "FAULT",response = boolean.class)
})
public boolean create(@RequestBody MaterialPo materialPo){
return this.smrMaterialService.createMaterial(materialPo);
}

@RequestMapping(value = "/saveorupdate",method = {RequestMethod.POST})
@ApiOperation(tags = {"SMR-Services"},value = "Material-保存或更新",notes = "根据\"id\"是否已存在来判断是保存还是更新")
@ApiImplicitParams({
@ApiImplicitParam(name = "materialPo", value = "资料报告对象", required = true, paramType = "body", dataType = "MaterialPo")
})
public boolean saveorupdate(@RequestBody MaterialPo materialPo){
return this.smrMaterialService.saveOrUpdateMaterial(materialPo);
}
}
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
最终显示结果为:

按照上述过程操作完,你就能得到一个swagger api document了,访问地址如下:
[ip]:[port]/../swagger-ui.html
本篇内容为对springfox的基本使用,细节将在日后慢慢补充。
---------------------
版权声明:本文为CSDN博主「焱魔王」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yzy199391/article/details/79913011

SpringMVC整合SpringFox实践总结的更多相关文章

  1. SpringMVC、SpringFox和Swagger整合项目实例

    目标 在做项目的时候,有时候需要提供其它平台(如业务平台)相关的HTTP接口,业务平台则通过开放的HTTP接口获取相关的内容,并完成自身业务~ 提供对外开放HTTP API接口,比较常用的是采用Spr ...

  2. eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis

    接上一篇: eclipse 创建maven 项目 动态web工程完整示例 eclipse maven工程自动添加依赖设置 maven工程可以在线搜索依赖的jar包,还是非常方便的 但是有的时候可能还需 ...

  3. MP实战系列(十)之SpringMVC集成SpringFox+Swagger2

    该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...

  4. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  5. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  6. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  7. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  9. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

随机推荐

  1. Python学习记录4-列表、元祖和集合

    list列表 一组由有序数据组成的序列 数据有先后顺序 数据可以不是一类数据 list的创建 直接创建,用中括号创建,内容直接用英文逗号隔开 使用list创建 列表包含单个字符串的时候是一个特例 # ...

  2. Mysql(五):索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  3. Redis使用总结-基础篇

    年底的时候开始尝试在重构的项目中使用redis,现在项目稳定运行也有一段时间了,这里做一下阶段性总结. 一.简介 首先,redis是什么意思呢,官方文档的FAQ里给出了答案:It means REmo ...

  4. layui多图上传实现删除功能

    在使用layui的多图上传时发现没有删除功能 在网上搜索解决办法时有的感觉太复杂有的不符合自己所需要的所以就自己动手 下面附上代码 HTML: <div class="layui-up ...

  5. 数据结构与算法——常用排序算法及其Java实现

    冒泡排序 原理:依次比较相邻的两个数,将小数放在前面(左边),大数放在后面(右边),就像冒泡一样具体操作:第一趟,首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前 ...

  6. 11 canvas 画布 - 基础

    一.概述 canvas它和其它的HTML5标签的使用基本一致,但是它相当于在浏览器中建立一个画布,可以再这个画布上画图.创建动画甚至是3D游戏.由于canvas要适配不同终端的分辨率,所以尽可能的在标 ...

  7. python--ctypes模块:调用C函数

    Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件 Windows 系统下的 C 标 ...

  8. 【AndroidStudio-添加RecyclerView包】 AndroidStudio添加v7包中的RecyclerView

    关于AndroidStudio如何添加v7包中的RecyclerView? 左侧Project视图,在External Libraries下找到appcompat-v7包 右击appcompat-v7 ...

  9. PHP mysqli_real_connect() 函数

    定义和用法mysqli_real_connect() 函数打开一个到 MySQL 服务器的新连接. mysqli_real_connect() 函数与 mysqli_connect() 函数在以下几个 ...

  10. CF1230 E. Kamil and Making a Stream gcd+暴力

    比赛的时候TLE,第二天发现合并方向合并错了~ 改了一下顺序就切了~ 又掉分了,好难过QAQ...... Code: #include <bits/stdc++.h> #define N ...