swagger2

简介

​ API Developmentfor Everyone。

​ Simplify API development for users, teams, and enterprises with the Swagger open source and professional toolset.

​ Find out how Swagger can help you design and document your APIs at scale.

​ 简单来说就是一个以springmvc注解驱动为主生成在线文档的工具。

常用网站

springmvc怎么引入swagger2

写在开头

swagger2引入的核心功能主要是如下三个:

在线接口界面/swagger-ui.html

​ 主要是把springfox-swagger-ui-2.6.1.jar包里的页面内容注册成请求

在线文档数据接口/v2/api-docs

​ 主要是把springfox.documentation.swagger2.web.Swagger2Controller注册如spirngmvc容器,此部分可以通过springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration进行整合

动态扫描注册接口的后台功能

​ 主要是针对springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration的整合

实操过程

引入jar包

备注:此处以spring4.1.5为基础引入,swagger2版本为2.6.1

classmate-1.4.0.jar
guava-18.0.jar
spring-plugin-core-1.2.0.RELEASE.jar
spring-plugin-metadata-1.2.0.RELEASE.jar
springfox-core-2.6.1.jar
springfox-spi-2.6.1.jar
springfox-schema-2.6.1.jar
springfox-spring-web-2.6.1.jar
springfox-swagger2-2.6.1.jar
springfox-swagger-common-2.6.1.jar
springfox-swagger-ui-2.6.1.jar
swagger-annotations-1.5.10.jar
swagger-models-1.5.10.jar

如上内容也可以通过maven引入

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
编写配置类
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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; /**
* @Description swagger配置类
* @Author chendeming
* @Date 2021/5/4
* @Version 1.0
**/
@Configuration
@EnableSwagger2
@Data
public class SwaggerConfig extends WebMvcConfigurationSupport {
// 是否开启swagger2,正式环境最好用false
private boolean enable = true;
private String scan = "扫包路径";
private String title = "应用名称";
private String description = "简要说明";
private String version = "1.0";
private String contract = "联系方式"; @Bean
public Docket api() {
// 创建注解
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
// .enable(isEnable())
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage(getScan()))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
// Contact contact = new Contact(getContractName(), getContractUrl(), getContractEmail());
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title(getTitle())
//文档描述
.description(getDescription())
//联系方式
.contact(contract)
//版本号
.version(getVersion())
.build();
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 注册swagger2页面资源
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
将配置类注册到容器中

这里需要注意:SwaggerConfig的bean一定要和@Controller、@RestController等请求放在同一个spring上下文中。

1、在springboot中引入

​ 直接用就行,保证springboot容器能够扫描到配置类SwaggerConfig即可

2、在springmvc中引入

​ 将SwaggerConfig以bean形式注册即可

3、在springmvc中引入,但是@Controller和@Service是分离注册的

​ 尤其注意!此处扫描@Controller和@Service的应该是两个xml,需要将SwaggerConfig以bean形式注册在扫描@Controller的那一个。

这个是扫描service的

<!-- scan service -->
<context:component-scan base-package="">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="assignable" expression="com.xxx.SwaggerConfig" />
</context:component-scan>

这个是扫描controller的

<context:component-scan base-package=""  use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<bean id="swaggerDocumentConfig" class="com.xxx.SwaggerConfig"/>

springmvc引入swagger2的更多相关文章

  1. 快速构建springmvc+spring+swagger2环境

    快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea               jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...

  2. SSM项目 以及 springboot 中引入swagger2的方法

    swagger2是一个非常好用的接口文档,在开发的过程中方便前后端接口的交接. 下面我们就来讲讲在使用java时,分别在SSM框架,以及springboot+mybatis框架中引入swagger2的 ...

  3. Decoration5:引入swagger2进行API管理

    这一部我们计划把swagger2引入到项目中,把网站的接口以文档的形式展示出来. 1.引入springfox-swagger2.springfox-swagger-ui 2.实现Swagger2 3. ...

  4. springMVC 引入静态资源Js的方式

    前两天项目出现了Js无法引入的情况,本篇博客先总结分析+批判自己犯的低级错,再说说几种访问静态资源的方式! 首先,由于在web.xml里面的servlet拦截匹配为<url-pattern> ...

  5. springmvc与swagger2

    首先呢我们导入相关的jar包文件 为了方便copy我copy一份 <!-- 导入java ee jar 包 -->        <dependency>           ...

  6. springMVC引入Validation详解

    本文简单介绍如何引入validation的步骤,如何通过自定义validation减少代码量,提高生产力.特别提及:非基本类型属性的valid,GET方法的处理,validation错误信息的统一re ...

  7. springMVC引入js,css文件404

    在web.xml中配置静态资源文件过滤 <!--静态文件引入--> <servlet-mapping> <servlet-name>default</serv ...

  8. mavn项目(springMVC) 引入静态资源(js、css)等

    在web.xml中配置 <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern ...

  9. springmvc引入静态资源文件

    如果web.xml中配置的DispatcherServlet请求映射为“/”, springmvc将捕获web容器所有的请求,当然也包括对静态资源的请求.springmvc会将他们当成一个普通请求处理 ...

  10. SpringMVC引入CSS等文件

    在默认情况下Spring MVC 拦截了所有请求,所以自己要把静态资源配置起来,IDEA 在Spring-service 配置,eclipse在自己新建的SpringMVC配置文件里配置,如下代码 & ...

随机推荐

  1. Java语言发展史-计算机进制转换

    Java语言发展史 java的诞生 在1991年时候,James Gosling在Sun公司的工程师小组想要设计这样一种主要用于像电视盒这样的消费类电子产品的小型计算机语言. 这些电子产品有一个共同的 ...

  2. (一)Abp入门

    ABP 是用于创建现代Web应用程序的完整体系结构和强大的基础架构,遵循最佳实践和约定,为 您提供 SOLID 开发经验. 目前 ABP 的版本   ASP.NET Boilerplate ASP.N ...

  3. FAQ 关于pip你应该知道的一些技巧

    pip简介 pip是安装了python之后的一个应用程序,包管理程序,有点类似于yum.npm.apt等工具 物理位置一般是python.exe所在目录下的scripts下 以我为例,我Python安 ...

  4. 鸣人的影分身(等级考试4级 2021-03 T3)

    题目: 此题题干又臭又长,直接看简化版. 鸣人的影分身(等级考试4级 2021-03 T3)等效于 把m个苹果分到n个盘子中,问有几种可能? dp[i][j]表示有i个盘子j个苹果时有多少种放法. 用 ...

  5. 破解练习-CRACKME002

    002-注册算法分析 一.工具和调试环境 动态调试工具:x64dbg 系统环境:win10 1909 二.分析Serial/name的算法 直接使用提示字符串验证,得到错误提示如下: 接下来使用x64 ...

  6. Vue09 事件

    1 事件语法 Vue 中的事件绑定可以使用 v-on 指令进行处理,可以把 v-on 绑定事件简写为 @. <div id="root"> <button @cl ...

  7. mysql-01数据库基本简介

    1.数据库的概念 DB:数据库(database):存储数据的"仓库".它保存了一系列有组织的数据. DBMS:数据库管理系统(Database Management System ...

  8. C++_bind用法

    1.bind函数 网络编程中, 经常要使用到回调函数. 当底层的网络框架有数据过来时,往往通过回调函数来通知业务层. 这样可以使网络层只专注于 数据的收发, 而不必关心业务 在c语言中, 回调函数的实 ...

  9. CMakeList汇总

    cmake_minimum_required(VERSION 2.8.3) PROJECT (HELLO) #工程名 set(CMAKE_BUILD_TYPE "Debug")se ...

  10. 推荐一款新的自动化测试框架:DrissionPage!

    今天给大家推荐一款基于Python的网页自动化工具:DrissionPage.这款工具既能控制浏览器,也能收发数据包,甚至能把两者合而为一,简单来说:集合了WEB浏览器自动化的便利性和 request ...