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生成接口文档代码的更多相关文章

  1. asp.net core 使用 swagger 生成接口文档

    参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...

  2. webapi 利用webapiHelp和swagger生成接口文档

    webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...

  3. .net core 使用 swagger 生成接口文档

    微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...

  4. asp.net core使用Swashbuckle.AspNetCore(swagger)生成接口文档

    asp.net core中使用Swashbuckle.AspNetCore(swagger)生成接口文档 Swashbuckle.AspNetCore:swagger的asp.net core实现 项 ...

  5. Django使用swagger生成接口文档

    参考博客:Django接入Swagger,生成Swagger接口文档-操作解析 Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.总体目标是使客户端和文 ...

  6. Go语言使用swagger生成接口文档

    swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...

  7. 用Swagger生成接口文档

    Swagger简介 在系统设计的时候,各个应用之间往往是通过接口进行交互的.因此接口的定义在整个团队中就变得尤为重要.我们可以把接口的规范用接口描述语言进行描述,然后Swagger可以根据我们定义的接 ...

  8. Spring Boot 集成 Swagger生成接口文档

    目的: Swagger是什么 Swagger的优点 Swagger的使用 Swagger是什么 官网(https://swagger.io/) Swagger 是一个规范和完整的框架,用于生成.描述. ...

  9. C# Swagger 生成接口文档

    一直听说Swagger是做Web API文档的好工具,这次手里暂时没什么事,类体验下它的强大之处.下面是使用Swashbuckle.net 给asp.net web API添加文档的简要步骤. 参考地 ...

随机推荐

  1. springboot外置的Servlet容器

    嵌入式Servlet容器:应用打成可执行的jar ​ 优点:简单.便携: ​ 缺点:默认不支持JSP.优化定制比较复杂(使用定制器[ServerProperties.自定义EmbeddedServle ...

  2. GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程

    GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程 https://www.sdbeta.com/mf/2018/1002/226048.html 软件下载: ...

  3. HTML第二章总结

    前言 在第一单元中,我们了解了 "ML":Markup Language,它的作用是: tell the strure of content;在这一单元,进一步认识 HT,它的作用 ...

  4. 20181013xlVba年级报表拆分为班级报表

    '年级报表拆分为班级报表 Public Sub CreateClassReport() Application.DisplayAlerts = False Dim Wb As Workbook Dim ...

  5. android -------- ConstraintLayout 宽高比和偏移量比(三)

    前面的文章 ConstraintLayout 介绍 (一) ConstraintLayout约束属性 (二) 此博文主要讲解: app:layout_constraintHorizontal_bias ...

  6. Spring Batch 使用场景

    一个标准的批处理程序通常会从数据库,文件或者队列中读取大量的数据和记录,然后对获取的数据进行处理,然后将修改后的格式写回到数据库中. 通常 Spring Batch 在离线模式下进行工作,不需要用户干 ...

  7. DRF之接口文档以及Xadmin

    1. 自动生成接口文档 REST framework可以自动帮助我们生成接口文档. 接口文档以网页的方式呈现. 自动接口文档能生成的是继承自APIView及其子类的视图. 1.1. 安装依赖 REST ...

  8. HTTP请求/响应报文结构

    HTTP协议版本有两种:HTTP1.0和HTTP1.1 它们俩的区别在于:HTTP1.0对于每个连接都只能传送一个请求和响应,请求后就会关闭,HTTP1.0没有Host字段:而HTTP1.1在同一个连 ...

  9. c++ shared_ptr的使用

    shared_ptr.是c++为了提高指针安全性而添加的智能指针,方便了内存管理.功能非常强大,非常强大,非常强大(不单单是shared_ptr,配合week_ptr以及enable_share_fr ...

  10. PHP 进阶之路 - 深入理解 FastCGI 协议以及在 PHP 中的实现

    在讨论 FastCGI 之前,不得不说传统的 CGI 的工作原理,同时应该大概了解 CGI 1.1 协议 传统 CGI 工作原理分析 客户端访问某个 URL 地址之后,通过 GET/POST/PUT ...