zoukankan      html  css  js  c++  java
  • JavaWeb项目中集成Swagger API文档

    1.增加依赖

    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>

    2.配置类SwaggerConfig.java

    package com.example.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    @ComponentScan(basePackages = { "com.example.demo" })
    public class SwaggerConfig {
        ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("demo Web service APIs")
                    .description("")
                    .license("")
                    .licenseUrl("")
                    .termsOfServiceUrl("")
                    .version("1.0.0")
                    .build();
        }
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                    .build()
                    .apiInfo(apiInfo());
        }
    
    }

    3.启动类加 @EnableSwagger2 注解

    package com.example;
    
    import com.example.config.SupplyConfig;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableConfigurationProperties(SupplyConfig.class)
    @EnableSwagger2
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    4.接口类加@Api及接口方法加@ApiOperation注解

    package com.example.demo.controller;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/greet")
    @Api(value = "demo api")
    public class HelloWorldController{
    
        @ApiOperation(value = "问好")
        @RequestMapping(value = "helloworld",method = RequestMethod.GET)
        public String sayHello() {
            return "hello world cc";
        }
    }

    5.访问swagger-ui

    http://localhost:8080/swagger-ui.html (端口是spring-boot项目启动的端口),打开页面如下:

    发送get请求:

  • 相关阅读:
    SQL语句之奇形怪状的冷门函数
    计算累计收益
    关于SQL表字段值缺失的处理办法
    虚拟机移植到另一台机器
    分分钟搞懂rank() over(partition by)的使用
    分分钟搞懂union与union all
    【转】10分钟就能学会的.NET Core配置
    【转】依赖注入的威力,.NET Core的魅力:解决MVC视图中的中文被html编码的问题
    【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
    【转】在.net Core 中像以前那样的使用HttpContext.Current
  • 原文地址:https://www.cnblogs.com/cici20166/p/8763969.html
Copyright © 2011-2022 走看看