• springBoot集成swagger2并使用


    导入依赖

    <dependency>
    		<groupId>io.springfox</groupId>
    		<artifactId>springfox-swagger2</artifactId>
    		<version>2.9.2</version>
    </dependency>
    <dependency>
    		<groupId>io.springfox</groupId>
    		<artifactId>springfox-swagger-ui</artifactId>
    		<version>2.9.2</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置swagger

    @Configuration
    @EnableSwagger2  //开启Swagger2
    public class SwaggerConfig {
    
        //配置Docket的bean实例
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                	//页面的描述信息
                    .apiInfo(apiInfo())
                    //开关Swagger 默认为true开启
                    .enable(true)
                    //设置组名
                    //若要实现多个组,则要配置多个Docket的bean实例
                    .groupName("zb")
                    .select()
                    /*apis()配置扫描接口的方式
                      basePackage()基于包扫描
                      any()扫描全部
                      none()不扫描
                      withClassAnnotation(Controller.class)扫描类上注解
                      withMethodAnnotation(GetMapping.class)扫描方法上注解*/
                    .apis(RequestHandlerSelectors.basePackage("com.zb.controller"))
                      /*paths()过滤什么路径
                      ant()只扫描这个路径下的
                      any()一个也不过滤
                      none()过滤全部
                      */
                    .paths(PathSelectors.none())
                    .build();
        }
    
        //配置Swagger信息 =>apiInfo()
        private ApiInfo apiInfo(){
            //作者信息
            Contact contact = new Contact("name","url","email");
    
            //DIY
            return new ApiInfo(
                    "Api Documentation",   //title
                    "Api Documentation",  //description
                    "1.0",  ///version
                    "urn:tos", // termsOfServiceUrl
                    contact,  //作者信息
                    "Apache 2.0",  //license
                    "http://www.apache.org/licenses/LICENSE-2.0", //licenseUrl
                     new ArrayList());
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    接口控制类

    @Api(tags = "控制类注释")
    @RestController
    public class HelloController {
    
        @ApiOperation("接口注释")
        @RequestMapping("/hello")
        public String index(){
            return "hello";
        }
    
        //返回值存在实体类,该实体类就会被扫描进Swagger中
        @RequestMapping("/user")
        public User user(){
            return new User();
        }
    
        @RequestMapping("/param")
        public String param(@ApiParam("参数注释") String param){
            return param;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    实体类

    @ApiModel("实体类注释")
    public class User {
        @ApiModelProperty("字段名id注释")
        private int id;
        @ApiModelProperty("字段名name注释")
        private String name;
    
        public User() {
        }
    
        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    【云原生系列】云计算概念与架构设计介绍
    深度学习入门(九) 多层感知机实现
    CSS实现文字跑马灯效果
    Python连接MariaDB数据库
    python计算长方形面积 青少年编程电子学会python编程等级考试一级真题解析2022年6月
    动态计算图笔记
    准备用HashMap存1W条数据,构造时传10000还会触发扩容吗?存1000呢?
    初识数据结构-B树
    什么是行内元素的盒模型
    基于SSM的物流系统
  • 原文地址:https://blog.csdn.net/m0_46400910/article/details/125617366