美文网首页程序员
SpringBoot进行整合Swagger2从此接口文档说拜拜

SpringBoot进行整合Swagger2从此接口文档说拜拜

作者: 墨迹嘿嘿 | 来源:发表于2020-04-19 16:06 被阅读0次

    SpringBoot进行Swagger的整合非常方便我们进行接口对接和调试。接口文档也不用给前端提供了,简直酸爽。。。。。

    我们首先需要引入Swagger的Jar包

    一、依赖

     <!--整合swagger-->
            <!-- swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <!-- swagger-ui -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
    

    二、Swagger配置类

    这个配置类你需要知道哪些东西能进行配置,我们可以抽出配置项,写到配置文件中,方便以后进行更改。 特别要注意的是里面配置了api文件也就是controller包的路径,不然生成的文档扫描不到接口。

    package com.herbert.demo.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    /**
     * Created by Herbert on 2020/4/15.
     */
    @Configuration
    public class SwaggerConfig {
    
        @Value("${swagger.enabled}")
        private boolean enabled;
    
        @Value("${swagger.title}")
        private String title;
    
        @Value("${swagger.base-package}")
        private String basePackAge;
    
        @Value("${swagger.description}")
        private String description;
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(basePackAge))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
        private ApiInfo apiInfo() {
            if(enabled){
                return new ApiInfoBuilder()
                    //页面标题
                    .title(title)
                    //描述
                    .description(description)
                    .build();
            }
            return new ApiInfoBuilder().build();
        }
    }
    
    

    在SpringBoot中用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。

    我们通过@Value将外部的值动态注入到Bean中,读取配置中的文件信息

    #swagger配置
    swagger:
      enabled: true
      title: 你知道吗?我是Title
      description: 描述:不想写描述.
      base-package: com.herbert.demo.controller
    

    三、开启Swagger配置

    Application.class 加上注解@EnableSwagger2 表示开启Swagger

    package com.herbert.demo;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * Created by Herbert on 2020/4/15.
     */
    
    @SpringBootApplication
    @MapperScan("com.herbert.demo.mapper") //扫描的mapper
    @EnableSwagger2
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
    }
    
    

    四、Restful 接口加入注解

    package com.herbert.demo.controller;
    
    import com.herbert.demo.entity.Test;
    import com.herbert.demo.service.TestService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    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;
    
    /**
     * Created by Herbert on 2020/4/15.
     */
    @Api(description = "测试接口")
    @RestController
    @RequestMapping("/test")
    @Slf4j
    public class TestController {
    
        @Autowired
        private TestService testService;
    
        @ApiOperation(value = "查询测试" ,  notes="查询用户信息")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", dataType = "String")
        })
        @RequestMapping(value="getTest",method= RequestMethod.GET)
        public Test GetUser(String id){
            return testService.findTest(id);
        }
    
    }
    
    

    启动SpringBoot项目,访问 http://localhost:8089/swagger-ui.html

    image image image

    相关文章

      网友评论

        本文标题:SpringBoot进行整合Swagger2从此接口文档说拜拜

        本文链接:https://www.haomeiwen.com/subject/hnombhtx.html