SpringBoot配置集成Swagger

现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处真的不是一点两点。下面我们看下如何在微服务中将springboot与swagger来结合吧。

1、swagger是什么,这个我觉得凡是一个开发人员就应该知道度娘啊,绝对强大。

简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后台的接口修改了后,swagger可以实现自动的更新,而不需要认为的维护这个接口进行测试。

2、springboot与swagger的集成:

第一步:jar包的引入:

<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>

第二步:swagger的配置启动类编写:

要使用swagger要进行一些配置,这个在界面的图上是可以显示的:类似于说明书:在这个类中我们会使用注解来进行启动swagger:

package com.aspirecn.sport.entity.common;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ProjectName: sport
 * @Author huangli
 * Swagger2配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aspirecn.sport.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("体育信息化服务Api")
                //描述
                .description("主要针对体育信息化客户端(Android|Ios)教师版和个人版提供数据接口")
                //创建人
                .contact(new Contact("huangli", "http://XXXX/login", "[email protected]"))
                .termsOfServiceUrl("NO terms of service")
                //版本号
                .version("1.0")
                .build();
    }
}

这样swagger2与springboot就集成完毕了。

看下最终效果吧:

扫描二维码关注公众号,回复: 5338447 查看本文章

访问路径:

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

猜你喜欢

转载自blog.csdn.net/huangli1466384630/article/details/86676911