springboot+swagger2

小序

新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行了改造。改造后的框架可以说能满足普通项目的所有需求,大家可以循环利用哈。后续我会附上摘出来的框架源码,和大家一起学习进步。今天主要是说一下springboot配置在线接口文档swagger2。

添加jar

在你的maven管理的项目的pom.xml中添加

 <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

添加swagger2的配置类

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
* Swagger2配置类 在与spring boot集成时,放在与Application.java同级的目录下。
* 通过@Configuration注解,让Spring来加载该类配置。 再通过@EnableSwagger2注解来启用Swagger2。
*/
@Configuration
@EnableSwagger2
public class Swagger2 { /**
* 创建API应用 apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.11.22.33.controller"))//此处改为你的接口所在的包路径
.paths(PathSelectors.any()).build();
} /**
* 创建该API的基本信息(这些基本信息会展现在文档页面中) 访问地址:http://项目实际地址/swagger-ui.html
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("title").description("description").contact("作者")
.version("1.0").build();//此处改为你自己的项目名称,描述,作者,版本
}
}

controller添加swagger2注解

  • 例一

多个参数的话,添加多个@ApiImplicitParam即可,逗号分隔。

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; @Api(value = "DeviceProductController|终端设备下的产品的相关接口")
@RequestMapping(RequestUrl.DEVEICE_PRODUCT_LIST)
@RestController
public class DeviceProductController {
@Autowired
private DeviceProductService deviceProductService; @ApiOperation(value="根据设备编号获取产品列表")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "deviceId", value = "设备编号", required = true, dataType = "String")
})
@RequestMapping(method = RequestMethod.GET)
String deveiceInfos(String deviceId, HttpServletRequest request) {
String result = deviceProductService.getDeviceProductList(deviceId);
return result;
}
}

  • 例二

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; @Api(value = "DeviceInfoController|终端设备的相关接口")
@RequestMapping(RequestUrl.DEVEICE_INFO_LIST)
@RestController
public class DeviceInfoController {
@Autowired
private DeviceInfoService deveiceInfoService; @ApiOperation(value="分页获取终端列表datatable的数据格式")
@RequestMapping(method = RequestMethod.GET)
String deveiceInfos(@ModelAttribute Gpage page, HttpServletRequest request) {
String result = deveiceInfoService.deveiceInfos(page, request);
return result;
}
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @ApiModel(value="分页对象类型")
@Data
public class Gpage{ @ApiModelProperty(value="每页显示个数" ,required=true)
private int length = 10;
@ApiModelProperty(value="开始坐标" ,required=true)
private int start;
}

在线接口文档访问路径

我本地启用的是8081端口,所以访问路径是http://localhost:8081/swagger-ui.html

遇到的坑

  • 如果你配置了spring.resources.static-locations,那你就需要把swagger-ui.html也要加进去

解决办法:新建类MyWebAppConfigurer,添加如下代码即可

 import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
/**
* 添加swagger-ui.html
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars*")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
  • 如果你配置了拦截器或者过滤器,也要过滤掉swagger的相关路径

springboot+swagger2的更多相关文章

  1. SpringBoot+Swagger2 整合

    SpringBoot+Swagger2四步整合 第一步:添加相关依赖 <parent> <groupId>org.springframework.boot</groupI ...

  2. Springboot+swagger2.7集成开发

    Springboot+swagger2.7集成开发 本篇文章是介绍最新的springboot和swagger2.7集成开发和2.0稍微有一些出入: Springboot集成环境配置 Swagger2. ...

  3. MP实战系列(八)之SpringBoot+Swagger2

    SpringBoot一个原则,爱好编程的朋友们都知道,那就是"习惯优于配置". 今天一上来主要说的还是代码,个人比较喜欢来的实战系列的,不过有的时候还是比较偏重于理论,理论是造轮子 ...

  4. springboot+swagger2案例

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  5. Springboot+swagger2的接口文档开发

    一.创建一个SpringBoot项目 1. 2. 3. 4. 把web里的web选中,SQL里选择自己需要的,点击next 二.创建各项所需的controller,configure等 1. 项目布局 ...

  6. SpringBoot + Swagger2 自动生成API接口文档

    spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...

  7. Springboot swagger2 导出api文档

    具体导出的代码,参考了:http://www.spring4all.com/article/699 导出前,首先需要配置好swagger2,参见 https://www.cnblogs.com/yan ...

  8. springboot + swagger2 生成api文档

    直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  9. springboot + swagger2 学习笔记

    引入步骤 1.添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springf ...

随机推荐

  1. Json安全

    1.不要使用顶级JSON数组,避免被<script>标签引用. 2.使用POST+密钥获取JSON,尽量不要用GET方式. 3.不要使用eval()将对象放入内存,eval()会执行所传入 ...

  2. FBI Warning

    FBI Warning... --------------------------- 电影中的片头部分:<正宗的FBI Warning>: ========== 翻译: fbi warni ...

  3. JavaScript函数使用和DOM节点

    一.函数的声明与调用 1.函数声明的格式: function 函数名(参数1, 参数2,....){ 函数体代码 return 返回值:} 2.函数的调用:①直接调用:函数名(参数1的值,参数2的值, ...

  4. linux(十三)之磁盘分区、创建文件系统、挂载

    前面学习了linux的用户管理 ,感觉是不是多lnux的多用户多任务的系统感觉十分了解了,但是其实并不然的.你还需要了解更多.接下来给大家分享的是 在vmware中添加硬盘创建分区,然后挂载到指定目录 ...

  5. Linux使用远程X Server显示图形

    背景 通常我们不希望在服务器上安装图形界面,但有时候有些程序需要图形界面,比如安装oracle的时候.此时,可以配置让Linux使用远程的X Server进行图形界面显示. 首先要明确的是Linux ...

  6. iOS 环信集成问题(连文档都不说明的坑。。)

    首先,关于环信SDK的下载和一些依赖库的添加,在此我就不做详细介绍,(http://www.easemob.com/download/im)附上环信官网文档,可以看一下,上面都可以下载,也有相关配置介 ...

  7. [转]Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom

     详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp84 Random即:java.util.Random, ThreadL ...

  8. RMI,RPC,SOAP对比分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp60   1.RMI     使用java的程序员,对于RMI(Remot ...

  9. Http get方式url参数长度以及大小

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp69 众所周知, 传递小量参数(在没有其他原因,例如隐藏参数值的情况下)推 ...

  10. poj 3177-3352边双联通

    买一送一啊  3177和3352的区别在于3177数据有重边!但是我先做3177的  那么就直接ctrl+c+v搞3352了~. 题意:给一个无向图,要令每个点之间至少有两条不重合的路,需要至少加多少 ...