介绍:

       Swagger是全球最大的OpenAPI规范(OAS)API开发工具框架,支持从设计和文档到测试和部署的整个API生命周期的开发。(摘自Swagger官网)Swagger说白了就是帮助开发者省去了维护接口文档的时间,用来调试接口非常方便。

一、创建项目

      在上一篇博客中介绍了怎么创建springboot项目,在这篇博客中就不过多讲,在博客的最后我会上传项目的源码,爱学习的小伙伴们可以下载交流,有问题的小伙伴可以在博客下方留言,博主看到后第一时间会给你回复。

二、引入swagger2依赖

 

    <!--swagger2-->
<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>

三、创建Swagger2配置类

package com.chaoqi.springboot_swagger.dao.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; //让Spring来加载该类配置
@Configuration
//开启Swagger2
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("HelloWorld")
.apiInfo(apiInfo())
.select()
// 扫描的包所在位置
.apis(RequestHandlerSelectors.basePackage("com.chaoqi.springboot_swagger.web"))
// 扫描的 URL 规则
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
// 联系信息
return new ApiInfoBuilder()
// 大标题
.title("")
// 描述
.description("")
// 服务条款 URL
.termsOfServiceUrl("")
// 版本
.version("")
.build();
}
}

创建controller

Swagger2注解说明:

@Api:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置" @ApiOperation:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明" @ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值 @ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类 @ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性
package com.chaoqi.springboot_swagger.web;

import com.chaoqi.springboot_swagger.dao.domain.MusicInfo;
import com.chaoqi.springboot_swagger.service.MusicInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
@RequestMapping(value = "/music")
@Api(tags = "音乐信息")
public class MusicInfoController { @Autowired
private MusicInfoService musicInfoService; @ApiOperation(value = "根据Id获取歌手信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", dataType = "Long"),
})
@RequestMapping(value = "/showMusic", method = RequestMethod.GET)
public List<MusicInfo> getMusicInfo(@RequestParam(name = "id", required = false) Long id) {
List<MusicInfo> musicInfo1 = musicInfoService.getMusicInfo(id);
return musicInfo1;
} @ApiOperation(value="根据Id删除歌手信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", dataType = "Long")
@RequestMapping(value="/deleteMusic", method=RequestMethod.DELETE)
public Long deleteUser(@RequestParam(name = "id", required = false) Long id) {
Long sum = musicInfoService.getDeleteId(id);
return sum;
}
}

启动项目运行http://localhost:8080/swagger-ui.html

整合成功,源码下载地址:https://github.com/caicahoqi/ChaoqiIsPrivateLibrary

springboot集成swagger2的更多相关文章

  1. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  2. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  3. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  4. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  5. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  6. springboot集成swagger2报Illegal DefaultValue null for parameter type integer

    springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...

  7. SpringBoot集成Swagger2 以及汉化 快速教程

    (一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...

  8. Springboot集成swagger2生成接口文档

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html    作者:jstarseven    码字挺辛苦的.....   一 ...

  9. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  10. [转] spring-boot集成swagger2

    经测,spring-boot版本使用1.5.2+时需使用springfox-swagger2版本2.5+(spring-boot 1.2 + springfox-swagger2 2.2 在未扫描ja ...

随机推荐

  1. AttributeError: 'TestLogin' object has no attribute 'driver' in Pycharm for python selenium

    自动化测试学习中的问题: 最近几天在写登陆测试,遇到一个问题,困惑我的几个小时......... 我各种百度,花费大量时间,才找到我的问题的根本所在,最终解决了我的问题,主要是大小写的问题def Se ...

  2. Linux定时器 timerfd使用

    英文使用手册原汁原味,一手资料. NAME       timerfd_create, timerfd_settime, timerfd_gettime - timers that notify vi ...

  3. GOF23种设计模式精解

    创建型 1. Factory Method(工厂方法) 2. Abstract Factory(抽象工厂) 3. Builder(建造者) 4. Prototype(原型) 5. Singleton( ...

  4. 数据结构--hashtable(散列表)

    散列 散列又叫hash.是通过关键字把数据映射到指定位置的一种数据结构.理想的散列表,是一个包含关键字的固定大小的数组 哈希表存储的是键值对,其查找的时间复杂度与元素数量多少无关,哈希表在查找元素时是 ...

  5. mysql数据库字符集编码查看以及设置

      show variables like %char% character_set_client     | gb2312                           character_s ...

  6. 2016弱校联盟十一专场10.2 Longest Increasing Subsequence

    这个dp题很有学问,我也是照着标称写的 还需要学习 补: if(order[i] < order[i-1]) pre[j] += now[j]; 这句的解释 首先order表示的是每个数字排序之 ...

  7. Radar Installation POJ - 1328

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  8. 页面jquery调试的一个宝贵经验(类似于Eclipse中的写出一个对象点它的方法时候用alt加/可以跳出来它所有的方法)

    案例讲解 一,html片段 <div class="page-upload"> <div class="tab-wrapper2"> & ...

  9. vs不支持通过afxgetmainwnd()获取窗口句柄(转)

    问题: 在vc6中这样代码顺利通过,可执行 ::SetDlgItemText(AfxGetMainWnd()-> m_hWnd,IDC_TIME,strTime); (这是在对话框程序中,代码在 ...

  10. xshell无法登录阿里云服务器

    1. 现象 a. 使用xshell无法登录服务器 b. 查看sshd服务 c. 不同公网ip的电脑正常登陆 2. 解决方案 a. 不同公网ip可以登录,断定是ip被黑名单,联系阿里云服务,把公网ip加 ...