为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 ...
随机推荐
- Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)
BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using na ...
- Go流程结构(if)
一.程序的流程结构 程序的流程控制结构一共有三种:顺序结构,选择结构,循环结构. 顺序结构:从上向下,逐行执行. 选择结构:条件满足,某些代码才会执行.0-1次 分支语句:if,switch,sele ...
- JS中constructor属性
constructor属性用于对当前对象的构造函数的引用.可以用来判断对象的类型: <script> var newStr = new String("One world One ...
- 方便的 IcoMoon 图标字体
官网地址:https://icomoon.io/app/#/select 已发现的方便之处: 1.官网已提供大量常用图标字体: 2.可通过 svg 将其转换为 图标字体: 3.不仅可转换,还可自定义编 ...
- EntityFramework 插入自增ID主从表数据
原因: 数据库中的两个表是主从表关系,但是没有建外键,而表的id用的是数据库的自增整数,导致在使用EF导入主从表数据时,需要先保存主表数据,取到 主表的自增id后才能插入从表数据,这样循环之下,数据插 ...
- HTML连载59-子绝父相
一.子绝父相 1.只使用相对定位,对图片的位置进行精准定位. <!DOCTYPE html> <html lang="en"> <head> & ...
- css中定义变量
css中定义变量 定义变量可分多种情况: 1.定义全局变量 :root { --borderColor: #ccc;} 2.定义某元素下的变量 .look{ --borderColor: #ccc;} ...
- A. Angry Students
网址:http://codeforces.com/problemset/problem/1287/A It's a walking tour day in SIS.Winter, so tt grou ...
- 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(05)
关于特性曲线的输出调整: 初代版本 material material=igzo eg300=3.5 nc300=8.5e21 nv300=8.5e21 taun0=1e-9 taup0=1e-9 a ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...