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;
    }

    }

     
  • 相关阅读:
    安全传输平台项目扩展——keymngserver重构-硬件扩展
    安全传输平台项目扩展——C复习-C++复习-keymngclient重构
    安全传输平台项目——客户端代码移植-项目模块总结
    安全传输平台项目——配置管理终端-读写数据库
    根号分治刷题记录
    使用netsh命令来管理IP安全策略
    关于make_shared无法访问非公有构造函数的解决方法
    两两交换链表中的节点-递归解法
    Spring 的 AOP 简介
    Spring IOC和DI 注解开发
  • 原文地址:https://www.cnblogs.com/lanyy/p/9084796.html
Copyright © 2011-2022 走看看