• 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
  • 相关阅读:
    [持续更新]计算机经典面试题基础篇Day2
    gcc编译工具
    Kafka中遇到的错误:
    开源软件的影响力及未来发展趋势
    Spring Boot | Spring Boot 默认 “缓存管理“ 、Spring Boot “缓存注解“ 介绍
    湖南省政协副主席赖明勇一行莅临麒麟信安调研
    1143. 最长公共子序列(C++实现)
    PostgreSQL创建数据库、数据库管理员用户、该库的只读用户
    Flutter 最佳实践 - 04
    Postman(3): postman发送POST请求
  • 原文地址:https://blog.csdn.net/m0_46400910/article/details/125617366