Springboot集成Swagger及使用

 我们的口号是:十年生死两茫茫,写代码,到天亮!!!

Swagger官网:API Documentation & Design Tools for Teams | Swagger

第一步: 

创建一个Springboot项目导入依赖:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

第二步:

写一个hello工程

@RestController
public class HelloContorller {

    @RequestMapping("hello")
    public String hello(){
        return "hello";
    }
}

第三步:

 启动后输入url访问地址:http://localhost:8080/swagger-ui/index.html

如果启动报此错:Failed to start bean 'documentationPluginsBootstrapper

那么就在配置文件添加:        

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

 Swagger信息设置

Swagger的bean实例Docket

package com.example.swaggertest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableSwagger2//开启Swagger
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());

    }

    public ApiInfo apiInfo(){
        return new ApiInfo(
                "喜羊羊的文档",//头部
                "十年生死两茫茫,写代码,到天亮",//描述
                "sb1.0",//版本
                "urn:tos",
                new Contact("喜羊羊","https:www.baidu.com/","443@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

Swagger接口设置

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(false)//是否启用swagger,false为不能再浏览器中访问
                .select()
                //RequestHandlerSelectors.basePackage("") 扫描指定的包
                //RequestHandlerSelectors.any() 扫描全部
                //RequestHandlerSelectors.none() 不扫描
                //RequestHandlerSelectors.withClassAnnotation(RestController.class) 扫描类上此注解的类,参数是一个注解的反射对象
                //RequestHandlerSelectors.withMethodAnnotation(GetMapping.class) 扫描方法上有此注解的方法
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                //.paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
        
    }

 

如何将Swagger在生产环境中使用,在发布的时候不使用?

1、判断是不是生产环境 。2、注入enable()

    @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())
                .enable(flag)//是否启用swagger,false为不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                .paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
    }

访问时要记得更改url中所用的环境的端口号进行访问。

Swagger组设置

@Configuration
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket4(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    @Bean
    public Docket docket(Environment environment){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("喜羊羊")
                .enable(true)//是否启用swagger,false为不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggertest.controller"))
                //.paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
    }

    //配置Swagger信息=apiInfo
    public ApiInfo apiInfo(){
        return new ApiInfo(
                "喜羊羊的文档",
                "十年生死两茫茫,写代码,到天亮",
                "sb1.0",
                "urn:tos",
                //作者信息
                new Contact("喜羊羊","https:www.baidu.com/","448510413@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

Swagger实体类信息设置

添加一个实体类

public class User {
    public int ID;
    public String Name;
    private int age;
}

添加一个接口

@RestController
public class HelloContorller {

    @RequestMapping("hello")
    public String hello1(){
        return "hello";
    }

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

 Swagger注解使用

 Controller中的注解:有很多,列出简单几个的使用

@RestController
@Api(tags = "hello控制层")
public class HelloContorller {

    @GetMapping("hello")
    public String hello1(){
        return "hello";
    }


    @ApiOperation("user的hello")
    @PostMapping("user/hello")
    public User hello2(@ApiParam("用户名") String name){
        return new User();
    }
}

 实体类中的注解:

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户id")
    public int ID;
    @ApiModelProperty("用户名")
    public String Name;
    @ApiModelProperty("年龄")
    private int age;
}

 Swagger扩展测试

@RestController
@Api(tags = "hello控制层")
public class HelloContorller {

    @GetMapping("hello")
    public String hello1(){
        return "hello";
    }


    @ApiOperation("user的hello")
    @PostMapping("user/hello")
    public String hello2(@ApiParam("用户") User user){
        String a = "成功";
        return a;
    }
}

 ok!!我们的口号是???​​​​​​​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值