zoukankan      html  css  js  c++  java
  • swagger

    swagger

    依赖

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    

    配置

    SwaggerConfig

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        //配置swagger的Docket的bean实例
        @Bean
        public Docket docket(Environment environment){
            //设置要显示的swagger环境
            Profiles profiles  = Profiles.of("dev", "test");
            //判断是否在已选中的环境中
            boolean flag = environment.acceptsProfiles(profiles);
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    //根据flag开关
                    .enable(flag)
                	//分组对应不同的Docket
                    .groupName("pinked")
                    //扫描路径
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("cn.pinked.swagger.controller"))
                    //.paths(PathSelectors.ant("/过滤的路径/**"))
                    .build();
        }
    
        //配置swagger信息-apiinfo
        public ApiInfo apiInfo(){
            //contact存放作者信息
            Contact contact = new Contact("野原新之助", "http://localhost:8080/", "abc@qq.com");
            return new ApiInfo(
                    "swagger api文档",
                    "这里放描述信息",
                    "v1.0",
                    "http://localhost:8080/",
                    contact,
                    "Apache 2.0",
                    "http://www.apache.org/licenses/LICENSE-2.0",
                    new ArrayList());
        }
    }
    

    yaml的多环境配置

    spring:
      profiles:
        active: test
    ---
    spring:
      profiles: test
    server:
      port: 8080
    ---
    spring:
      profiles: prod
    server:
      port: 8081
    ---
    spring:
      profiles: dev
    server:
      port: 8082
    

    接口注释

    User

    @ApiModel("用户实体类")
    public class User {
        @ApiModelProperty("用户名")
        public String name;
        @ApiModelProperty("密码")
        public String password;
    }
    

    controller

    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello() {
            return "hello swagger";
        }
    
        @PostMapping("/user")
        public User user() {
            return new User();
        }
    
        @ApiOperation("post测试")
        @PostMapping("/postt")
        public User postt(@ApiParam("用户") User user) {
            int i = 5 / 0;
            return user;
        }
    }
    

    总结

    • 可以为难理解的属性或接口添加注释信息
    • 接口文档实时更新
    • 可以在线测试
  • 相关阅读:
    Gnome-keyring总是提示输入密码问题
    ubuntu下制作u盘启动盘
    ubuntu下制作u盘启动盘
    comgt拨号
    linux命令学习笔记
    U-Boot启动过程完全分析
    从Linux程序中执行shell(程序、脚本)并获得输出结果
    Git fetch和git pull的区别
    php如何获取远程文件内容的函数
    php验证邮箱和ip地址方法总汇
  • 原文地址:https://www.cnblogs.com/pinked/p/12383686.html
Copyright © 2011-2022 走看看