swagger2是一个非常好用的接口文档,在开发的过程中方便前后端接口的交接。

下面我们就来讲讲在使用java时,分别在SSM框架,以及springboot+mybatis框架中引入swagger2的方法。

一、在SSM框架中引入swagger2

 

需要加的maven依赖

 

1.在com.imooc.utils下创建一个swagger2的配置类

package com.imooc.utils;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.web")) //指明controller所在的包
.paths(PathSelectors.any()).build();
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("短视频后台管理系统api接口文档")
// 设置联系人
.contact(new Contact("联系人姓名", "see more at..","邮箱"))
// 描述
.description("controller层接口如下")
// 定义版本号
.version("1.0").build();
}
}

2.在springmvc.xml中进行配置


 

二、在springboot项目中引入swagger2

 

添加maven依赖

 

1.在com.imooc包下创建swagger2的配置类

 

package com.imooc;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

/**
* @Description:swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.controller"))
.paths(PathSelectors.any()).build();
}

/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("使用swagger2构建短视频后端api接口文档")
// 设置联系人
.contact(new Contact("联系人姓名", "see more at..","联系人的电子邮箱 "))
// 描述
.description("controller层接口如下")
// 定义版本号
.version("1.0").build();
}

}

 

2.在springboot的启动类中指明需要扫描的包,只要让swagger2.java被扫描到即可


 

3.在springboot的WebMvcConfig类中配置允许静态资源的访问

 

============================================================================

 

通过上面的方法,我们就已经在SSM项目或者springboot项目中成功引入了swagger2 接口文档。

此时我们就可以通过 ip+端口/swagger-ui.html 来访问接口文档,例如 http://localhost:8080/swagger-ui.html

====================================================================

在引入了swagger2之后,我们还需要加上一些注解,才能够达到我们想要的效果,swagger2中常用注解的使用:

https://blog.csdn.net/ajklaclk/article/details/80736042

 

 

 

 

 

SSM项目 以及 springboot 中引入swagger2的方法的更多相关文章

  1. vue-cli中引入jquery的方法

    vue-cli中引入jquery的方法 以前写vue项目都没有引入过jquery,今天群里面的一位小伙伴问了我这个问题,我就自己捣鼓了一下,方法如下: 我们先进入webpack.base.conf.j ...

  2. HTML中引入CSS的方法

    在HTML中引入CSS的方法主要有四种,它们分别是行内式.内嵌式.链接式和导入式. 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用. 2.内嵌式 ...

  3. 在springboot中使用swagger2

    1.在springboot中使用swagger的话,首先在pom文件中引入依赖 <!-- https://mvnrepository.com/artifact/io.springfox/spri ...

  4. vue-cli3.0以上项目中引入jquery的方法

    这里配置的是vue-cli3.0引入jquery的方法,不是vue-cli2.0的配置方法 一.安装jquery npm install jquery --save 二.在vue.config.js ...

  5. CSS系列:在HTML中引入CSS的方法

    HTML与CSS是两个作用不同的语言,它们同时对一个网页产生作用,因此必须将CSS与HTML链接在一起使用.在HTML中,引入CSS的方法主要有4种:行内式.内嵌式.导入式和链接式. 1. 行内式 行 ...

  6. vue中引入awesomeswiper的方法以及编写轮播组件

    1.先安装less-loader npm install less less-loader --save 2.再安装css-loader npm install css-loader --save 3 ...

  7. 在SpringBoot中引入Redis

    前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...

  8. SpringBoot中集成Swagger2

    1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  9. SpringBoot中部署Swagger2和Swagger-UI

    1 Gradle配置在dependencies中添加以下依赖: implementation("io.springfox:springfox-swagger2:2.7.0") im ...

随机推荐

  1. 设置完代理IP,手机连接WiFi时连不上,一直在转

    fiddler抓包,设置代理后,会出现手机连不上网的情况,针对这个情况处理方式为: 首先如果已经连接上WiFi了,但是提示“不可上网”,那就把WiFi断开重新连接下. 不行的话就试试下面的办法 一.配 ...

  2. Win10安装4 —— 通过BIOS进入PE

    本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12369367.html 一:"BIOS"与"PE& ...

  3. 在SQL中怎么把一列字符串拆分为多列

    --首先,你是按什么规则拆? 我举个例子 你要按字段中的逗号拆开,假设字段名叫text --用charindex和substring这2个函数    select substring(text,1,c ...

  4. LED Keychain-Ideal For Mass Promotions

    Looking for something memorable to remind people of your business or nonprofit? Consider custom LED ...

  5. 小I选宾馆

    小 I 选宾馆 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小 I 去天津玩啦,一路上,他跟他的同学发生了许多有趣的 ...

  6. static静态不是很静

    在类中定义变量时,不会开辟存储空间,只有类定义一个对象时才会开辟类中成员变量的内存空间,且建立一个对象开辟一次,大小与类中的成员变量及函数有关.而static在静态区开辟内存空间,不占用内存空间. 1 ...

  7. ProgressTimer 控件

    let background = new cc.Sprite(fileName_background);this.addChild(background,999999);background.setP ...

  8. hadoop搭建HA集群之后不能自动切换namenode

    在搭好HA集群之后,想测试一下集群的高可用性,于是先把active的namenode给停掉: hadoop-daemon.sh stop namenode 或者直接kill掉该节点namenode的对 ...

  9. c语言修炼之一

    1.C项目要高内聚(模块功能必须明确,一个模块完成一个功能).低耦合(接口尽可能简单,减少各模块间的联系). 2.register类型不能为模块间的全局变量.模块内的全局变量.局部static变量.( ...

  10. echats--visualmap

    visualmap 既图片左下角的筛选按钮 1.对颜色的区分 visualMap: [ { top: 20, left: 0, right: null, // 设置文本为红色 textStyle: { ...