为spring boot 写的Controller中的rest接口配置swagger
1.pom.xml文件中加入下列依赖:
<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>
2.创建一个Swagger2类:
//对swagger的配置
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))//扫描接口的包
.paths(PathSelectors.regex("/rest/.*"))//被文档展示的接口的地址,这里只展示rest/地址下的
.build();
} public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("spring boot利用swagger构建api文档")
.description("简单优雅的rest风格")
.termsOfServiceUrl("http://www.maycpou.com")//文档遵循的开发协议的展现网址
.version("1.0")//版本
.build();
}
}
3.启动项目后访问http://localhost:8888/swagger-ui.html
4.针对swagger2中还有很多的注解写在接口或者传入传出参数上面,来添加在swagger文档界面增加对该接口或者参数的说明如:
接口方法:
@ApiOperation(value = "添加文章",notes = "添加新的文章",tags = "Article",httpMethod = "POST")//方法的swagger注释
// @ApiImplicitParams({
// @ApiImplicitParam(name = "id" ,value = "文章Id",required = true,dataType = "String"),
// @ApiImplicitParam(name = "name" ,value = "文章名称",required = true,dataType = "String"),
// })//这个注解是针对请求参数是@RequestParam方式的时候,可以给每个参数添加swagger文档上的注释
@ApiResponses({//方法返回值的swagger注释
@ApiResponse(code = 200,message = "成功",response = AjaxResponse.class),
@ApiResponse(code = 400,message = "用户输入错误",response = AjaxResponse.class),
@ApiResponse(code = 500,message = "系统内部错误",response = AjaxResponse.class)
})
@RequestMapping(value = "/article", method = RequestMethod.POST,produces = "application/json")
//上面的注解等同于@PostMapping("/article")
public AjaxResponse saveArticle(@RequestBody Article article){//使用@RequestBody的方式接收参数,可以自动的将传入的json转化装配为对象
//如果使用@RequestParam的方式接受参数就要将Article对象里面的字段全部写在传入参数中
log.info("saveArticle:{}",article);
return AjaxResponse.success();
} 传出参数:
@Data
@ApiModel//用于swagger的接口返回实体
public class AjaxResponse {
@ApiModelProperty("是否成功")//swagger中显示的返回字段的注释
private boolean isok;
private int code;
private String message;
private Object data;
private AjaxResponse(){ } public static AjaxResponse success(){
AjaxResponse resultBean = new AjaxResponse();
resultBean.setIsok(true);
resultBean.setCode(200);
resultBean.setMessage("success");
return resultBean;
} public static AjaxResponse success(Object data){
AjaxResponse resultBean = new AjaxResponse();
resultBean.setIsok(true);
resultBean.setCode(200);
resultBean.setMessage("success");
resultBean.setData(data);
return resultBean;
}
}
为spring boot 写的Controller中的rest接口配置swagger的更多相关文章
- 安装使用Spring boot 写一个hello1
一.创建springboot 项目 二.进行代编写 1.连接数据库:application.properties里配置 spring.datasource.driverClassName=com.my ...
- Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value
错误场景 用Spring boot写了一个简单的RESTful API,在测试POST请求的时候,request body是一个符合对应实体类要求的json串,post的时候报错. 先贴一段error ...
- Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去
1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...
- spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。
需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@ 而不是 ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- 【spring boot】SpringBoot初学(3)– application配置和profile隔离配置
前言 github: https://github.com/vergilyn/SpringBootDemo 说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各 ...
- Spring Boot (七): Mybatis极简配置
Spring Boot (七): Mybatis极简配置 1. 前言 ORM 框架的目的是简化编程中的数据库操作,经过这么多年的发展,基本上活到现在的就剩下两家了,一个是宣称可以不用写 SQL 的 H ...
- Spring Boot 2.X(五):MyBatis 多数据源配置
前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...
- Java | Spring Boot Swagger2 集成REST ful API 生成接口文档
Spring Boot Swagger2 集成REST ful API 生成接口文档 原文 简介 由于Spring Boot 的特性,用来开发 REST ful 变得非常容易,并且结合 Swagg ...
随机推荐
- js 时间格式化工具类
/** * 返回示例:0 天 4 小时 7 分钟 57 秒 * @param second 毫秒数 * @returns {String} 时间html */ function secondToDay ...
- Go_runtime包
package main import ( "fmt" "runtime" "time" ) //写在init函数里,main函数运行之前就 ...
- c# 异常:值不能为 null。 参数名: source
异常详细信息: System.ArgumentNullException: 值不能为 null.参数名: source 其实问题那就出在 Select() 方法,在 Select 上按 F12 查看定 ...
- python 读写函数
1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.tx ...
- eclipse 鼠标悬停提示
如果想要关闭鼠标悬停提示,只要把Window --> Preferences... --> Java --> Editor --> Hovers 把 Combined Hove ...
- 压缩和解压工具bandizip
同质化的压缩软件 提及 Windows 平台的压缩软件,大家往往想起老牌的 WinRAR.开源免费的 7-Zip.国产的快压.好压.360 压缩之类,甚至还有时代的眼泪 WinZip.一直以来,压缩软 ...
- Linux下载安装
博客及下载 https://www.cnblogs.com/nongzihong/p/10475753.html centos镜像 下载 https://blog.csdn.net/sinat_365 ...
- ES-windows版本设置远程访问
1,官网下载 2,下载完解压 3,修改配置文件 elasticsearch.yml network.host: 0.0.0.0http.port: 9200transport.host: localh ...
- cc攻击怎么防御,如何防止cc攻击?
当我们访问一个网站时,如果网站页面越简单,访问速度越快,页面越漂亮,加载速度就越慢,因为要加载更多东西,服务器压力也会比较大.cc攻击就是利用这种弱点,使用大量代理服务器,对网站进行攻击,消耗网站服务 ...
- 跨域-JSONP
jsonp跨域 - 前端适配,后台配合 说明:前后台同时改造 cnpm i jsonp --save-dev 在App.vue里 import jsonp from 'jsonp' let url = ...