swagger

swagger

api框架

RestFul Api文档在线自动生成工具=>Api文档与 Api定义同步更新

直接运行,可以在线测试Api接口

支持多种语言

官方网站:https://swagger.io/

在项目中的Swagger需要springbox

swagger2

ui

maven坐标:

  <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

配置spring boot的集成:

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//配置swagger
@Configuration
@EnableSwagger2
public class Swagger {
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-77GiMih6-1607926775496)(image-20201214121116165.png)]

访问路径:http://localhost:8080/swagger-ui.html

//配置了swagge
@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo());
}
//配置swagger信息
private ApiInfo apiInfo(){
    Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
  return   new ApiInfo("Swagger的api文档",
          "xxxxx",
          "1.0", 
          "baidu.com",
          DEFAULT_CONTACT,
          "Apache 2.0",
          "http://www.apache.org/licenses/LICENSE-2.0",
          new ArrayList());

}

swagger配置扫描接口

//配置了swagge
    //enable是否启用swagger
@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //basePackage  扫描指定的包
            //any;  扫描全部
            //none;  不扫描
            //withclassAnnotation;  扫描类上的主键
            //withMetAnnotation
            .apis(RequestHandlerSelectors.basePackage("com.swagger.controoler"))
            //过滤路径
            .paths(PathSelectors.ant("/com/***"))
            .build();
}
//配置swagger信息
private ApiInfo apiInfo(){
    Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
  return   new ApiInfo("Swagger的api文档",
          "xxxxx",
          "1.0",
          "baidu.com",
          DEFAULT_CONTACT,
          "Apache 2.0",
          "http://www.apache.org/licenses/LICENSE-2.0",
          new ArrayList());

}

让swagger只在生成环境使用?

swagger的配置:

package com.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

//配置swagger
@Configuration
@EnableSwagger2
public class Swagger {

    //配置了swagge
    //enable是否启用swagger
    @Bean
    public Docket docket(Environment environment){
        //设置显示的swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //通过enviroment。acceptsProfiles 判断是否处在自己设定的环境当中
        boolean flag=environment.acceptsProfiles(profiles);
         //获取项目的环境;


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.controoler"))
                //过滤路径
               // .paths(PathSelectors.ant("/com/***"))
                .build();
    }
    //配置swagger信息
    private ApiInfo apiInfo(){
        Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
      return   new ApiInfo("Swagger的api文档",
              "xxxxx",
              "1.0",
              "baidu.com",
              DEFAULT_CONTACT,
              "Apache 2.0",
              "http://www.apache.org/licenses/LICENSE-2.0",
              new ArrayList());

    }


}

分组:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mOpjZiga-1607926775498)(Untitled/image-20201214134538482.png)]

swagger的controller的使用:

@RestController
public class HelloController {

    @GetMapping("/hello")
 public String hello(){
     return "hello";
 }
    @ApiOperation("User的控制")
   //  返回值存在实体类  ta就会 扫描到swagger中去
    @PostMapping("/user")
    public String user(@ApiParam("用户名") String username){
        return username;
    }
}

实体类的注释:

@ApiModel("用户实体类")  //加注释
public class User {
    @ApiModelProperty("用户名")
     private String username;
    @ApiModelProperty("用户密码")
     private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

总结:

正式开发的时候不要关闭swagger的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度搜索爱敲代码的小庆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值