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. shell基础命令知识持续更新

    查看系统支持的shell cat /etc/shells [root@iZwz9almo8p830btq7voo9Z shellLearning]# cat /etc/shells /bin/sh / ...

  2. SSM中PageHelper的使用方法

    SSM中PageHelper的使用方法 转载于for dream 第一步.导包(或者导入坐标) <!-- https://mvnrepository.com/artifact/com.githu ...

  3. 11月29日内容总结——SQL注入问题、视图、触发器、事务、存储过程、函数、流程控制、索引、慢查询、数据库三大范式

    目录 一.SQL注入问题 SQL注入问题引入 SQL注入概念和解决方案 二.视图 三.触发器 定义 代码 1.触发器命名有一定的规律 2.临时修改SQL语句的结束符 四.事务 事务的四大特性(ACID ...

  4. C-08\变量类别和名称粉碎机制

    全局变量 定义:在所有函数外部定义的变量称为全局变量,一般以g_开头,如 char g_szBuf[100]; // 全局变量g_szBuf int main() { printf("%s\ ...

  5. 关于Mysql外键从新学习

    关于Mysql外键从新学习 参考:https://blog.csdn.net/u010373419/article/details/9321331 说实话,这是一个抄剩饭的文档. 为什么会从新学习外键 ...

  6. 力扣每日一题2023.1.16---1813. 句子相似性 III

    一个句子是由一些单词与它们之间的单个空格组成,且句子的开头和结尾没有多余空格.比方说,"Hello World" ,"HELLO" ,"hello w ...

  7. Spring02---IOC-Debug查看Bean的实例化过程

    1 简介 springIOC它是对bean进行管理. 我们通常可以通过xml.properties.yml.注解等来配置bean的信息 spring读取这些配置信息,解析,生成BeanDefiniti ...

  8. IOS12 + Xscode12 报错:Building for iOS Simulator, but linking in dylib built for iOS, file '/Users/XXX/Desktop/XXXX/XXX.framework/JSSDK' for architecture arm64

    问题描述:编译过程出现错误,Building for iOS Simulator, but linking in dylib built for iOS, file '/Users/XXX/Deskt ...

  9. shrio

    Shrio 页面报错 <link rel="shortcut icon" href="#"/> 简单的安全框架 官网:https://shiro.a ...

  10. Yapi安装配置(CentOs)

    环境要求 nodejs(7.6+) mongodb(2.6+) git 准备工作 清除yum命令缓存 sudo yum clean all 卸载低版本nodejs yum remove nodejs ...