zoukankan      html  css  js  c++  java
  • 用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;
    }

    }

     
  • 相关阅读:
    C# 枚举常用工具方法
    AppBox_v3.0
    DDD:四色原型中Role的 “六” 种实现方式和PHP的Swoole扩展
    MySql主从配置实践及其优势浅谈
    ActionInvoker
    【Oracle】-【体系结构】-【DBWR】-DBWR进程相关理解
    Linux MySQL单实例源码编译安装5.6
    窗口嵌入到另一个窗口(VC和QT都有)
    Window下 Qt 编译MySQL驱动(居然用到了动态库格式转换工具,需要将C:/MySQL/lib目录下的libmySQL.dll文件复制到我们Qt Creator安装目录下的qt/bin目录中)good
    在Linux下使用iconv转换字符串编码
  • 原文地址:https://www.cnblogs.com/lanyy/p/9084796.html
Copyright © 2011-2022 走看看