• 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
  • 相关阅读:
    数据结构—快速排序(续)
    J2EE--自定义mvc增删改查
    计算机毕业设计ssm基于web的暗香小店系统的设计与实现80041系统+程序+源码+lw+远程部署
    VSFTPD2.3.4(笑脸漏洞)复现
    SparkCore系列-8、RDD 读写外部数据源
    MVCC究竟是什么?
    Unity3d-异步加载场景、进度条加载
    【Python百日进阶-Web开发-Peewee】Day279 - SQLite 扩展(四)
    面试官问:Flink内部数据在算子链内外如何交换?Flink为何能高效处理数据?你该怎么回答呢?
    Hibernate知识大合集
  • 原文地址:https://blog.csdn.net/m0_46400910/article/details/125617366