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. C# 读取配置文件方法

    如 xml中写: <?xml version="1.0" encoding="utf-8" ?> <config> <serv_i ...

  2. English trip EM2-LP-6B Teacher:Gabriele

    Gabriele    Gabi               n. 加布里尔,加布里埃尔,加布里埃,加布里埃莱(人名) 课上内容(Lesson) 词汇(Key Word ) is married? 结 ...

  3. jquery选择器和基本操作

    定义:jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法: ...

  4. Python记录_day21 模块

    引入模块的方式: 1. import 模块 2. from xxx import 模块 一.collections 模块 1.Counter() counter是一个计数器,主要用来计数,计算一个字符 ...

  5. poj2891 扩展中国剩余定理

    求a1x1+r1=y...anxn+rn=y,crt合并 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optim ...

  6. 子类父类(虚函数下的 引用指针 对象)->看来没有子类指针这回事

    #include<iostream> using namespace std; class Father { public: Father() { cout << " ...

  7. Highcharts 配置选项详细说明

    Highcharts 配置选项详细说明 Highcharts 提供大量的配置选项参数,您可以轻松定制符合用户要求的图表,本章节为大家详细介绍Highcharts 配置选项使用说明: 参数配置(属性+事 ...

  8. Leetcode 144

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. linux删除文件后不释放磁盘的问题

    1. 用df 检查发现根目录可用空间越为200M # df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde1 .9G .4G 232M ...

  10. Python装饰器--decorator

    装饰器 装饰器实质是一个函数,其作用就是在不改动其它函数代码的情况下,增加一些功能.如果我们需要打印函数调用前后日志,可以这么做 def log(func): print('%s is running ...