美文网首页
swagger学习文档

swagger学习文档

作者: 磨陀货_ | 来源:发表于2019-05-13 15:02 被阅读0次
旭旭提供

为什么需要swagger

因为接口文档管理非常麻烦,用过swagger,只需要配置一点点,就可以通过网页很好的管理。

导入jar
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

本项目配置
package cn.zx.xuxu.product.config;

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;

@Configuration
@EnableSwagger2
public class Swagger2 {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //唯一要改的地方就是这里的路径  其他不管
                .apis(RequestHandlerSelectors.basePackage("cn.zx.xuxu.product.controller"))
                //包:就是自己接口的包路径
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("商品系统api")//名字
                .description("商品系统接口文档说明")//额外藐视
                .contact(new Contact("wbtest", "", "wenbing@zx.cn"))
                .version("1.0")// 版本
                .build();
    }

}

本项目地址:http://127.0.0.1:8001/swagger-ui.html

旭旭提供
网关配置

前端开发每次都要记录每个服务ip不好,期望只记忆网关ip就ok,需要做配置

  • 网关中配置
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  • 建个新类。在网关中
package cn.zx.xuxu.config;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
       //aigou网关前缀,employee网关路径   ;swagger的路径 ,以后增加了接口就在这配置就ok
        resources.add(swaggerResource("用户系统", "/xuxu/user/v2/api-docs", "2.0"));
        resources.add(swaggerResource("商品系统", "/xuxu/product/v2/api-docs", "2.0"));
        resources.add(swaggerResource("基础系统", "/xuxu/common/v2/api-docs", "2.0"));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
package cn.zx.xuxu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("分布式购物系统")
                .description("购物系统接口文档说明")
                .termsOfServiceUrl("http://localhost:8081")
                .contact(new Contact("wbtest", "", "wenbing@itsoruce.cn"))
                .version("1.0")
                .build();
    }

}
图片.png 图片.png

相关文章

网友评论

      本文标题:swagger学习文档

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