springboot-swagger

在这里插入图片描述
springboot-swagger准备:

springboot2.x
  <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
  Springfox Swagger UI      
        <!-- 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>

springboot3.0之后只需要导入这个就可以:
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

springboot2.x访问: http://localhost:8080/swagger-ui/

springboot3.x访问:

原 swagger-ui.html 已变更,
当前版本url http://localhost:8080/swagger-ui/ 或 http://localhost:8080/swagger-ui/index.html

1.配置Swagger

1、Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。

@Bean //配置docket以配置Swagger具体参数
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2);

2、可以通过apiInfo()属性配置文档信息(自定义配置)

//配置文档信息
private ApiInfo apiInfo() {
    Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
    return new ApiInfo(
            "Swagger学习", // 标题
            "学习演示如何配置Swagger", // 描述
            "v1.0", // 版本
            "http://terms.service.url/组织链接", // 组织链接
            contact, // 联系人信息
            "Apach 2.0 许可", // 许可
            "许可链接", // 许可连接
            new ArrayList<>()// 扩展
    );
}

3、Docket 实例关联上 apiInfo()

@Bean
public Docket docket() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}

4、重启项目,访问测试 http://localhost:8080/swagger-ui/ 或 http://localhost:8080/swagger-ui/index.html**看下效果;

2.配置扫描接口

1、构建Docket时通过select()方法配置扫描接口。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FSt7U2wg-1601084819057)(C:\Users\lxz\AppData\Roaming\Typora\typora-user-images\image-20200925201445824.png)]

properties配置选择多环境

配置swagger的Docket的bean实例
@Bean
public Docket docket() {
    //            设置显示swagger的环境,如果为dev或者test则可以进入网站里
        Profiles profile = Profiles.of("dev","test");

//        environment.acceptsProfiles判断是否自己处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profile);
    
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(able)//是否启用swagger,为false,则不可以在浏览器中访问swagger
        .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
        .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
        .path(PathSelectors.ant("/example/**"))//过滤路径扫描example下的路径
        .build();
}

3.分组和接口注释以及小结

@Configuration
@EnableSwagger2  //开启swagger
public class Swaggerconfig {

//    一个docket就是一个实例,对应着一个api接口,必须得自己去配置
  @Bean
  public Docket docket1()
  {
      return new Docket(DocumentationType.SWAGGER_2).groupName("分组1");
  }
  @Bean
  public Docket docket2()
  {
      return new Docket(DocumentationType.SWAGGER_2).groupName("分组2");
  }
  @Bean
  public Docket docket3()
  {
      return new Docket(DocumentationType.SWAGGER_2).groupName("分组3");
  }

  //配置了swagger的bean实例
  @Bean
  public Docket docket (Environment environment)
  {
//            设置显示swagger的环境
      Profiles profile = Profiles.of("pro","test");

//        environment.acceptsProfiles判断是否自己处在自己设定的环境当中
      boolean flag = environment.acceptsProfiles(profile);

      return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(flag).select().apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller")).build();
  }


  private ApiInfo apiInfo()
  {
      Contact contact = new Contact("lxz","www.http://baidu.com","1250680052@qq.com");
      return new ApiInfo("Api Documentation",
              "Api Documentation",
              "1.0",
              "urn:tos",
              contact,
              "Apache 2.0",
              "http://www.apache.org/licenses/LICENSE-2.0",
              new ArrayList()
      );

  }
}

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

controller类:

@Api(tags = "hellocontroller")//给类加注解
@RestController
public class TestController {

    @ApiOperation("hello控制类")//这个是给方法加注释的
    @RequestMapping("/hello")
    public String hello(@ApiParam("用户名")String name)//@ApiParam("用户名")给参数加注解
    {
        return "hello world"+name;
    }

    //只要接口中返回值中存在实体类,就会被扫描到swagger中
    @RequestMapping("/user")
    public User user()
    {
        return new User();
    }
}

在这里插入图片描述
在这里插入图片描述
出于安全考虑:发布时可以关闭swagger,页节省内存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值