zoukankan      html  css  js  c++  java
  • swagger案例Swagger案例

    pom

      <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <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.8.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.6</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.10</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    View Code

    controller

    @RestController
    @Api(value = "test", description = "测试demo")
    public class DemoController {
    
        @ApiOperation(value = "根据ID查询", notes = "传入id")
        @RequestMapping(value = "/test1", method = RequestMethod.GET)
        public String getTest(String id) {
            System.out.println(id);
            Gson gson = new Gson();
            List<String> list = new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("3");
            Demo demo = new Demo();
            return gson.toJson(list);
        }
    }
    View Code

    swagger启动

    @Configuration
    @EnableSwagger2
    public class SwaggerApp {
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //为当前包路径
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                    .paths(PathSelectors.any())
                    .build();
    //        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
        }
    
        //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    //页面标题
                    .title("Spring Boot 使用 Swagger2 构建RESTful API")
                    //创建人
                    .contact(new Contact("Bryan", "http://blog.bianxh.top/", ""))
                    //版本号
                    .version("1.0")
                    //描述
                    .description("API 描述")
                    .build();
        }
    
    }
    View Code

    默认测试访问地址

    http://localhost:8080/swagger-ui.html

  • 相关阅读:
    ASP.net注意点小结
    AJAX笔记
    为Dreamweaver写的扩展—jQuery自动提示.
    Cisco PIX防火墙配置上
    Cisco PIX防火墙配置下
    基于DNS的负载均衡
    c#使用文本框TextBox使用ctr+enter直接跳到指定Button
    Google marker
    C#中的关键字详解
    android中,锁屏之后是不是会关掉部分服务?怎么让我的服务不被关掉?
  • 原文地址:https://www.cnblogs.com/qq376324789/p/12055207.html
Copyright © 2011-2022 走看看