SpringBoot+Swagger2
SpringBoot与Swagger2整合
- 依赖:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency>
</dependencies>
- SwaggerConfig:
package com.blu.config;import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RequestMapping;
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 SwaggerConfig {public static final Contact BLU_CONTACT = new Contact("BLU", "http://localhost:8080", "736917155@qq.com");/*** 配置了Swagger的Docket的bean实例*/@Beanpublic Docket docket(Environment environment) {//设置需要启动Swagger的环境Profiles profiles = Profiles.of("dev","test");//通过acceptsProfiles方法判断是否处于指定的环境中boolean flag = environment.acceptsProfiles(profiles);return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())//是否启动Swagger,默认为true,这里通过flag来确保在开发环境启动,在发布环境关闭.enable(flag)//设置分组名,(通过创建不同的 Docket Bean 来实现分组).groupName("BLU组").select()//配置扫描接口的方式,basePackage指定要扫描的包.apis(RequestHandlerSelectors.basePackage("com.blu.controller"))//依据指定的注解来扫描//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//指定扫描的url//.paths(PathSelectors.ant("/hello/**")).build();}@Beanpublic Docket docketA() {return new Docket(DocumentationType.SWAGGER_2).groupName("A组");}/*** 自定义apiInfo*/private ApiInfo apiInfo() {return new ApiInfo("BLU的SwaggerApi文档","即使再小的帆也能远航","1.0","http://localhost:8080",BLU_CONTACT,"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList());}}
- application.properties
spring.profiles.active=dev
- application-dev.properties
server.port=8080
- application-pro.properties
server.port=8081
- User实体类:
package com.blu.entity;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;@ApiModel("用户实体类")
public class User {@ApiModelProperty("用户ID")private int id;@ApiModelProperty("用户名")private String username;@ApiModelProperty("年龄")private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
- HelloController
package com.blu.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.blu.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;@Api(tags="HelloController控制类")
@RestController
public class HelloController {@ApiOperation("HelloController控制类中的Hello()方法")@RequestMapping(value="/hello")public String Hello() {return "hello";}@ApiOperation("HelloController控制类中的user()方法")@PostMapping("/user")public User user() {return new User();}@ApiOperation("HelloController控制类中的Hello2()方法")@GetMapping("/hello2")public String Hello2(@ApiParam("Hello2()方法中的username参数") @RequestParam String username) {return "hello"+username;}}
- 启动类:
package com.blu;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringbootSwaggerApplication {public static void main(String[] args) {SpringApplication.run(SpringbootSwaggerApplication.class, args);}}
- 启动后,访问:http://localhost:8080/swagger-ui.html
SpringBoot+Swagger2相关推荐
- SpringBoot+Swagger2常用注解
场景 SpringBoot+Swagger2实现可视化API文档流程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/936166 ...
- SpringBoot+Swagger2.7.0实现汉化(2.8.0不行)
场景 SpringBoot+Swagger2实现可视化API文档流程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/936166 ...
- Springboot swagger2教程
swagger2教程 swagger,中文"拽"的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试.另外swagger很容 ...
- SpringBoot+Swagger2实现可视化API文档流程
场景 swagger官网: https://swagger.io/ 效果 会将所有的接口Controller可视化显示 点击其中的Controller显示具体的接口方法,左边是请求的方式,中间是接口的 ...
- SpringBoot+Swagger2实现自动生成API文档
Swagger概述 Swagger是一组围绕OpenAPI规范构建的开源工具,可帮助设计.构建.记录和使用REST API. 简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现 ...
- No mapping for GET /swagger-ui.html Springboot+swagger2 报错解决方法
1.报错 添加资源映射配置后报错:No mapping for GET /swagger-ui.html package com.ziyang.config;import org.springfram ...
- SpringBoot+Swagger2.7进行接口测试后台无法获取请求参数
场景 使用Swawgger2.7的UI页面进行接口测试时,传递参数,后端的方法无法获取请求的参数. 请求参数页面 后台接受参数的方法 后端完整代码 @Description("废料包材回收入 ...
- SpringBoot 使用Swagger2打造在线接口文档(附源代码)
点击上方"好好学java",选择"置顶公众号" 优秀学习资源.干货第一时间送达! 精彩内容 java实战练习项目教程 2018微服务资源springboot.s ...
- SpringBoot+MyBatisPlus+Swagger2.7规范开发接口流程(以废料包材入库与出库为例)
场景 SpringBoot+Swagger2实现可视化API文档流程: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/936166 ...
最新文章
- 利用组策略防止计算机访问共享资源
- ArcGIS Desktop 9.3.1安装流程
- TLD文件中body-content四种类型(能力工场)
- 实用ExtJS教程100例-004:等待对话框Ext.MessageBox.wait
- es6-promise源码重点分析难点解析
- Python手册 3.7
- 在sublime text里阅读ABAP源代码的一些例子
- Android Glide图片加载框架(四)回调与监听
- cv2.cornerHarris()详解 python+OpenCV 中的 Harris 角点检测
- python3 定时任务_Python3.x:定时任务实现方式
- Docker-端口映射实现访问容器
- 模糊聚类划分matlab代码,模糊C均值聚类算法(原理+Matlab代码) - 全文
- Linux之nslookup命令使用详解—网络故障定位(一)
- Nova 组件如何协同工作 - 每天5分钟玩转 OpenStack(24)
- xps13蓝牙消失,设备管理器中出现“未知USB设备(设备描述符请求失败)”的问题
- MBP TouchBar自定义使用
- excel数据库_标签打印软件中Excel数据整理及导入
- 安卓/Android 点击按钮/返回键跳转返回到 手机系统桌面
- Google Android软件架构
- java实现第六届蓝桥杯生命之树
热门文章
- mysql之分页查询
- 视频编解码(十六):VE解码器解码demo解码流程
- 视频编解码(十四):机顶盒调试编解码器显示总结
- Vue项目中如何实现用户登录及token验证?
- 计算机网络技术专业一体化课程方案,计算机网络技术专业网络设备的安装与配置...
- android预览界面显示不全,Android SurfaceView Camera 预览显示不全(画面拉伸)
- Centos7安装JDK8以及环境配置
- 少和这种人在一起,不管你是谁!
- python锁机制_Python并发编程之谈谈线程中的“锁机制”(三)
- 阿里云asp主机 后台登录一直提示验证码错误_拖欠阿里云一分钱,结果?