用swagger生成接口文档代码
1、Swagger2类:
package com.example.demo;
import com.google.common.base.Predicate; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
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; @Configuration
@EnableSwagger2
public class Swagger2 {
// @ApiOperation(value = "获取指定id用户详细信息",
// notes = "根据user的id来获取用户详细信息",
// httpMethod = "GET")
// @ApiImplicitParams({
// @ApiImplicitParam(name = "userName", value = "用户名",
// paramType = "query", required = true, dataType = "String"),
// @ApiImplicitParam(name = "password", value = "用户密码",
// paramType = "query", required = true, dataType = "String")
// }) // @Api //:注解controller,value为@RequestMapping路径
//
// @ApiOperation //:注解方法,value为简要描述,notes为全面描述,hidden=true Swagger将不显示该方法,默认为false
//
// @ApiParam //:注解参数,hidden=true Swagger参数列表将不显示该参数,name对应参数名,value为注释,defaultValue设置默认值,allowableValues设置范围值,required设置参数是否必须,默认为false
//
// @ApiModel //:注解Model
//
@ApiModelProperty //:注解Model下的属性,当前端传过来的是一个对象时swagger中该对象的属性注解就是ApiModelProperty中的value @ApiIgnore //:注解类、参数、方法,注解后将不在Swagger UI中显示 @Bean
public Docket createRestApi() {
Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
@Override
public boolean apply(RequestHandler input) {
if (input.isAnnotatedWith(ApiOperation.class)) {
return true;
}
return false;
}
};
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) // document info
.select()
.apis(predicate)
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("buzhou match")
.description("buzhou exchange match system")
.license("Apache License Version 2.0")
.version("1.0")
.build();
}
} 2、TestController类:
package com.example.demo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
// private static Logger logger = LogManager.getLogger(TestController.class);
@ApiOperation(value="根路径", httpMethod="GET", notes="这是一个hello world")
@GetMapping("/")
public String getStr() {
// logger.info("****Hello World!****");
System.out.println("****Hello World!****");
return "结果是:返回helloworld";
}
@ApiOperation(value="异常", httpMethod="GET", notes="这是一个除0的异常")
@GetMapping("/zeroException")
public int ZeroException() {
return 100/0;
}
@ApiOperation(value="获取年龄", httpMethod="GET", notes="获取年龄接口")
@GetMapping("/getage")
public int getage(int age) {
return age;
}
private int age;
@ApiOperation(value="设置年龄", httpMethod="GET", notes="设置年龄接口")
@GetMapping("/setage")
public void setAge(int age) {
this.age = age;
}
}
用swagger生成接口文档代码的更多相关文章
- asp.net core 使用 swagger 生成接口文档
参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...
- webapi 利用webapiHelp和swagger生成接口文档
webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...
- .net core 使用 swagger 生成接口文档
微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...
- asp.net core使用Swashbuckle.AspNetCore(swagger)生成接口文档
asp.net core中使用Swashbuckle.AspNetCore(swagger)生成接口文档 Swashbuckle.AspNetCore:swagger的asp.net core实现 项 ...
- Django使用swagger生成接口文档
参考博客:Django接入Swagger,生成Swagger接口文档-操作解析 Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.总体目标是使客户端和文 ...
- Go语言使用swagger生成接口文档
swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...
- 用Swagger生成接口文档
Swagger简介 在系统设计的时候,各个应用之间往往是通过接口进行交互的.因此接口的定义在整个团队中就变得尤为重要.我们可以把接口的规范用接口描述语言进行描述,然后Swagger可以根据我们定义的接 ...
- Spring Boot 集成 Swagger生成接口文档
目的: Swagger是什么 Swagger的优点 Swagger的使用 Swagger是什么 官网(https://swagger.io/) Swagger 是一个规范和完整的框架,用于生成.描述. ...
- C# Swagger 生成接口文档
一直听说Swagger是做Web API文档的好工具,这次手里暂时没什么事,类体验下它的强大之处.下面是使用Swashbuckle.net 给asp.net web API添加文档的简要步骤. 参考地 ...
随机推荐
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格添加分页
jQuery EasyUI 树形菜单 - 树形网格添加分页 本教程展示如何向带有动态加载特性的树形网格(TreeGrid)添加分页. 创建树形网格(TreeGrid) 启用树形网格(TreeGrid) ...
- English Voice of <<All Of Me>>
"All Of Me"我的一切 [Verse 1:]What would I do without your smart mouth没有你的蜜语甜言,我该怎办Drawing me ...
- 20170921xlVBA_SQL蒸发循环查询2
'ARRAY("1991","1992","1993","1994","1996","19 ...
- vux, vue如何控制微信自带的返回按钮,让其返回其他页面?
<script> import { mapState } from 'vuex' export default{ name: 'clockFx', data () { return { } ...
- 解决Requests中文乱码【有用】,读取htm文件 读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0
打开这个网址https://blog.csdn.net/chaowanghn/article/details/54889835 python在open读取txt文件时,出现UnicodeDecodeE ...
- tomcat从manager部署war项目上传失败
tomcat从manager部署war项目上传失败, 查看manager.2018-07-17.log 日志,可以看到如下信息. less manager.2018-07-17.log 17-Jul- ...
- 【LeetCode】最大子序列和
要求时间复杂度 O(n). e.g. 给定数组 [-2,1,-3,4,-1,2,1,-5,4],其中有连续子序列 [4,-1,2,1] 和最大为 6. 我完全没有想法,看了答案. C++实现: int ...
- 前端web的图标的样式
- LSTM UEBA异常检测——deeplog里其实提到了,就是多分类LSTM算法,结合LSTM预测误差来检测异常参数
结合CNN的可以参考:http://fcst.ceaj.org/CN/article/downloadArticleFile.do?attachType=PDF&id=1497 除了行为,其他 ...
- chrome google mozilla firefox bookmarks import export
chrome导出导入bookmarks 1◆ google帐号 自己申请,脑补 2◆ google访问 脑补 suggestion Lantern 3◆ 步骤 4◆ 导入 sample ...