zoukankan      html  css  js  c++  java
  • springboot整合swagger2

    直接copy代码   pom.xml

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
        <!-- 管理依赖 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.M7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- SpringBoot整合eureka客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!-- swagger2 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.8.0</version>
            </dependency>
        </dependencies>
        <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/libs-milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>

    配置

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                    // api扫包
                    .apis(RequestHandlerSelectors.basePackage("com.itmayiedu.api")).paths(PathSelectors.any()).build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("每特教育|蚂蚁课堂 微服务电商系统").description("每特教育|蚂蚁课堂 Java分布式&微服务培训")
                    .termsOfServiceUrl("http://www.itmayiedu.com")
                    // .contact(contact)
                    .version("1.0").build();
        }
    
    }

    控制层

    package com.mj.api;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @Api(tags = "控制层")
    public class MJController {
    
        @GetMapping("/index")
        @ApiOperation("index方法")
        public String index(){
            return "majie666";
        }
    
        @DeleteMapping("/delete")
        public String delete(){
            return "shanchu";
        }
    }

    访问地址:http://localhost:8060/swagger-ui.html

  • 相关阅读:
    Algs4-2.4.26无需交换的堆
    Algs4-2.4.25 计算数论
    OPNET IT Guru 学术版下载安装注册步骤(Modeler Academic Edition)
    LintCode Python 入门级题目 365.二进制有多少个1; 181.将整数A转换为B
    LintCode Python 简单级题目 491.回文数
    LintCode Python 简单级题目 2.尾部的零
    LintCode Python 困难级题目 20.骰子求和 动态规划
    LintCode Python 简单级题目 464.整数排序 II
    LintCode Python 简单级题目 165.合并两个排序链表
    LintCode Python 简单级题目 423.有效的括号序列
  • 原文地址:https://www.cnblogs.com/a1304908180/p/11373197.html
Copyright © 2011-2022 走看看