1.add all backend swagger api in web gateway

main
Gary 11 months ago
parent cf6ea1fda2
commit 77a89dc245

@ -41,6 +41,14 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -0,0 +1,52 @@
package com.luoo.web.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
@Order(value = 1)
public Docket groupRestApi() {
//https://blog.csdn.net/LitongZero/article/details/103822643
List<ResponseMessage> responseMessageList =new ArrayList<>();
responseMessageList.add(new ResponseMessageBuilder().code(20000).message("成功").responseModel(new ModelRef("成功")).build());
responseMessageList.add(new ResponseMessageBuilder().code(20001).message("失败").responseModel(new ModelRef("失败")).build());
responseMessageList.add(new ResponseMessageBuilder().code(20002).message("用户名或密码错误").responseModel(new ModelRef("用户名或密码错误")).build());
responseMessageList.add(new ResponseMessageBuilder().code(20003).message("无权访问").responseModel(new ModelRef("无权访问")).build());
responseMessageList.add(new ResponseMessageBuilder().code(20009).message("重复提交").responseModel(new ModelRef("重复提交")).build());
return new Docket(DocumentationType.SWAGGER_2)
.globalResponseMessage(RequestMethod.GET, responseMessageList)
.globalResponseMessage(RequestMethod.POST, responseMessageList)
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
.apiInfo(groupApiInfo());
}
private ApiInfo groupApiInfo(){
return new ApiInfoBuilder()
.title("luoo backend API")
.description("luoo 后端接口文档")
.contact(new Contact("王卿", "", ""))
.version("1.0")
.build();
}
}

@ -0,0 +1,52 @@
/*
* Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
* All rights reserved.
* Official Web Site: http://www.xiaominfo.com.
* Developer Web Site: http://open.xiaominfo.com.
*/
package com.luoo.web.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
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 SwaggerResourceConfig implements SwaggerResourcesProvider {
Logger logger= LoggerFactory.getLogger(SwaggerResourceConfig.class);
@Autowired
RouteLocator routeLocator;
@Override
public List<SwaggerResource> get() {
//获取所有router
List<SwaggerResource> resources = new ArrayList<>();
List<Route> routes = routeLocator.getRoutes();
logger.info("Route Size:{}",routes.size());
for (Route route:routes) {
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs")));
}
return resources;
}
private SwaggerResource swaggerResource(String name, String location) {
logger.info("name:{},location:{}",name,location);
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("2.0");
return swaggerResource;
}
}
Loading…
Cancel
Save