diff --git a/swagger-bootstrap-ui-gateway/pom.xml b/swagger-bootstrap-ui-gateway/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..2c83adc4071c48ccde332cae4991331b9ec3dcc5 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + + com.xiaominfo.swagger + swagger-bootstrap-ui-gateway + 1.0 + pom + Spring Cloud Gateway集成Springfox-Swagger以及Swagger-bootstrap-ui项目 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + service-server + service-user + service-order + service-doc + + + UTF-8 + UTF-8 + 1.8 + Finchley.RELEASE + + 1.9.3 + + 2.9.2 + 1.5.21 + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-integration + + + + org.springframework.integration + spring-integration-webflux + + + + + + io.springfox + springfox-swagger2 + ${springfox.version} + + + io.swagger + swagger-models + + + + + + + io.swagger + swagger-models + ${swagger.version} + + + + + + io.springfox + springfox-bean-validators + ${springfox.version} + + + + + + + jcenter-snapshots + jcenter + http://oss.jfrog.org/simple/oss-snapshot-local/io/springfox/ + + false + + + true + + + + + \ No newline at end of file diff --git a/swagger-bootstrap-ui-gateway/service-doc/pom.xml b/swagger-bootstrap-ui-gateway/service-doc/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..33d14301103171aacc4e51b1d7e4f378d4f846d5 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + com.xiaominfo.swagger + swagger-bootstrap-ui-gateway + 1.0 + ../pom.xml + + service-doc + service-doc + 文档 + + + 1.8 + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-data-redis-reactive + + + + io.springfox + springfox-swagger-ui + ${springfox.version} + + + + com.github.xiaoymin + swagger-bootstrap-ui + ${sbu.version} + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/ServiceDocApplication.java b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/ServiceDocApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..4fededeb61e788d80dae16964b789a2180b9c0a7 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/ServiceDocApplication.java @@ -0,0 +1,17 @@ +package com.xiaominfo.swagger.service.doc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; + +@EnableDiscoveryClient +@EnableEurekaClient +@SpringBootApplication +public class ServiceDocApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceDocApplication.class, args); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerHeaderFilter.java b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerHeaderFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..89e169c25d315eeadb10ee7151ab2e9dbdd06472 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerHeaderFilter.java @@ -0,0 +1,36 @@ +package com.xiaominfo.swagger.service.doc.config; + +import org.apache.commons.lang.StringUtils; +import org.springframework.cloud.gateway.filter.GatewayFilter; +import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; + + +/** + * @author fsl + * @description: SwaggerHeaderFilter + * @date 2019-06-0310:47 + */ +@Component +public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory { + private static final String HEADER_NAME = "X-Forwarded-Prefix"; + + private static final String URI = "/v2/api-docs"; + + @Override + public GatewayFilter apply(Object config) { + return (exchange, chain) -> { + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + if (!StringUtils.endsWithIgnoreCase(path,URI )) { + return chain.filter(exchange); + } + String basePath = path.substring(0, path.lastIndexOf(URI)); + ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build(); + ServerWebExchange newExchange = exchange.mutate().request(newRequest).build(); + return chain.filter(newExchange); + }; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerResourceConfig.java b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerResourceConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..b08c06c997c25e75554acfc90162768b5c1366e8 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/config/SwaggerResourceConfig.java @@ -0,0 +1,66 @@ +/* + * 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.xiaominfo.swagger.service.doc.config; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.config.GatewayProperties; +import org.springframework.cloud.gateway.route.RouteLocator; +import org.springframework.cloud.gateway.support.NameUtils; +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; + +/*** + * + * @since:swagger-bootstrap-ui 1.0 + * @author fsl + * 2019/05/04 12:38 + */ +@Slf4j +@Component +@Primary +@AllArgsConstructor +public class SwaggerResourceConfig implements SwaggerResourcesProvider { + + private final RouteLocator routeLocator; + private final GatewayProperties gatewayProperties; + + + @Override + public List get() { + List resources = new ArrayList<>(); + List routes = new ArrayList<>(); + routeLocator.getRoutes().subscribe(route -> routes.add(route.getId())); + gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> { + route.getPredicates().stream() + .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) + .forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(), + predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0") + .replace("**", "v2/api-docs")))); + }); + + return resources; + } + + private SwaggerResource swaggerResource(String name, String location) { + log.info("name:{},location:{}",name,location); + SwaggerResource swaggerResource = new SwaggerResource(); + swaggerResource.setName(name); + swaggerResource.setLocation(location); + swaggerResource.setSwaggerVersion("2.0"); + return swaggerResource; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/handler/SwaggerHandler.java b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/handler/SwaggerHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..a4700d8d60a81149b2fa311c3f2e910c01d04dc3 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/main/java/com/xiaominfo/swagger/service/doc/handler/SwaggerHandler.java @@ -0,0 +1,52 @@ +package com.xiaominfo.swagger.service.doc.handler; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; +import springfox.documentation.swagger.web.*; + +import java.util.Optional; + + +/** + * @author fsl + * @description: swagger-resource + * @date 2019-06-0310:47 + */ +@RestController +public class SwaggerHandler { + + @Autowired(required = false) + private SecurityConfiguration securityConfiguration; + + @Autowired(required = false) + private UiConfiguration uiConfiguration; + + private final SwaggerResourcesProvider swaggerResources; + + @Autowired + public SwaggerHandler(SwaggerResourcesProvider swaggerResources) { + this.swaggerResources = swaggerResources; + } + + + @GetMapping("/swagger-resources/configuration/security") + public Mono> securityConfiguration() { + return Mono.just(new ResponseEntity<>( + Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK)); + } + + @GetMapping("/swagger-resources/configuration/ui") + public Mono> uiConfiguration() { + return Mono.just(new ResponseEntity<>( + Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK)); + } + + @GetMapping("/swagger-resources") + public Mono swaggerResources() { + return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK))); + } +} diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/main/resources/application.yml b/swagger-bootstrap-ui-gateway/service-doc/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..f9cec619b2b4c1ad79f5ce3fe026efd27fb1855b --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/main/resources/application.yml @@ -0,0 +1,37 @@ +server: + port: 10003 +spring: + application: + name: service-doc + cloud: + gateway: + discovery: + locator: + # enabled: true + lowerCaseServiceId: true + routes: + - id: service-user + uri: lb://service-user + predicates: + - Path=/user/** + # - Header=Cookie,Set-Cookie + filters: + - SwaggerHeaderFilter + - StripPrefix=1 + - id: service-order + uri: lb://service-order + predicates: + - Path=/order/** + filters: + # - SwaggerHeaderFilter + - StripPrefix=1 + + +eureka: + client: + serviceUrl: + defaultZone: http://localhost:10000/eureka/ + +logging: + level: + org.springframework:cloud.gateway: debug \ No newline at end of file diff --git a/swagger-bootstrap-ui-gateway/service-doc/src/test/java/com/xiaominfo/swagger/service/doc/ServiceDocApplicationTests.java b/swagger-bootstrap-ui-gateway/service-doc/src/test/java/com/xiaominfo/swagger/service/doc/ServiceDocApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..5f32c6b5435982ba255216c57a37c96ace2e5533 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-doc/src/test/java/com/xiaominfo/swagger/service/doc/ServiceDocApplicationTests.java @@ -0,0 +1,16 @@ +package com.xiaominfo.swagger.service.doc; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ServiceDocApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-order/pom.xml b/swagger-bootstrap-ui-gateway/service-order/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..b86a96ed8647696f0f19050a414b64a167656a42 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.xiaominfo.swagger + swagger-bootstrap-ui-gateway + 1.0 + ../pom.xml + + service-order + service-order + Order Service + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/ServiceOrderApplication.java b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/ServiceOrderApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..23832dd7032923d2b6e223a5b535256ab6e7c5b3 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/ServiceOrderApplication.java @@ -0,0 +1,38 @@ +package com.xiaominfo.swagger.service.order; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.Environment; + +import java.net.InetAddress; +import java.net.UnknownHostException; + + +@EnableEurekaClient +@SpringBootApplication +public class ServiceOrderApplication { + + static Logger logger= LoggerFactory.getLogger(ServiceOrderApplication.class); + + public static void main(String[] args) throws UnknownHostException { + ConfigurableApplicationContext application=SpringApplication.run(ServiceOrderApplication.class, args); + Environment env = application.getEnvironment(); + logger.info("\n----------------------------------------------------------\n\t" + + "Application '{}' is running! Access URLs:\n\t" + + "Local: \t\thttp://localhost:{}\n\t" + + "External: \thttp://{}:{}\n\t"+ + "Doc: \thttp://{}:{}/doc.html\n"+ + "----------------------------------------------------------", + env.getProperty("spring.application.name"), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port")); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/common/Rest.java b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/common/Rest.java new file mode 100644 index 0000000000000000000000000000000000000000..2a54d72696b4a1d9a13345d2a07267f2490d786d --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/common/Rest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 Zhejiang BYCDAO Technology CO.,LTD. + * All rights reserved. + * Official Web Site: http://www.bycdao.com. + * Developer Web Site: http://open.bycdao.com. + */ + +package com.xiaominfo.swagger.service.order.common; + +import io.swagger.annotations.ApiModelProperty; + +/*** + * + * @since:swagger-bootstrap-ui-demo 1.0 + * @author xiaoymin@foxmail.com + * 2017/08/21 16:41 + */ +public class Rest { + + @ApiModelProperty(value = "是否成功") + private boolean success=true; + @ApiModelProperty(value = "返回对象") + private T data; + @ApiModelProperty(value = "错误编号") + private Integer errCode; + @ApiModelProperty(value = "错误信息") + private String message; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public Integer getErrCode() { + return errCode; + } + + public void setErrCode(Integer errCode) { + this.errCode = errCode; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/config/SwaggerConfiguration.java b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/config/SwaggerConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..5a8477cb6667882186ea4cc923bbfc01d2fd57d7 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/config/SwaggerConfiguration.java @@ -0,0 +1,86 @@ + +package com.xiaominfo.swagger.service.order.config; + +import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; +import com.google.common.collect.Lists; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.annotation.Order; +import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +import java.util.List; + + +@Configuration +@EnableSwagger2 +@EnableSwaggerBootstrapUI +@Import(BeanValidatorPluginsConfiguration.class) +public class SwaggerConfiguration { + + @Bean(value = "orderApi") + @Order(value = 1) + public Docket groupRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(groupApiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.xiaominfo.swagger.service.order.controller")) + .paths(PathSelectors.any()) + + .build().securityContexts(Lists.newArrayList(securityContext(),securityContext1())).securitySchemes(Lists.newArrayList(apiKey(),apiKey1())); + } + + private ApiInfo groupApiInfo(){ + return new ApiInfoBuilder() + .title("swagger-bootstrap-ui很棒~~~!!!") + .description("
swagger-bootstrap-ui-demo RESTful APIs
") + .termsOfServiceUrl("http://www.group.com/") + .contact("group@qq.com") + .version("1.0") + .build(); + } + + + + private ApiKey apiKey() { + return new ApiKey("BearerToken", "Authorization", "header"); + } + private ApiKey apiKey1() { + return new ApiKey("BearerToken1", "Authorization-x", "header"); + } + + private SecurityContext securityContext() { + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .forPaths(PathSelectors.regex("/.*")) + .build(); + } + private SecurityContext securityContext1() { + return SecurityContext.builder() + .securityReferences(defaultAuth1()) + .forPaths(PathSelectors.regex("/.*")) + .build(); + } + + List defaultAuth() { + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes)); + } + List defaultAuth1() { + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Lists.newArrayList(new SecurityReference("BearerToken1", authorizationScopes)); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/controller/OrderController.java b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/controller/OrderController.java new file mode 100644 index 0000000000000000000000000000000000000000..f44ff2a1ae65e6f37e2a68f5e8c2f44ae905e248 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/controller/OrderController.java @@ -0,0 +1,53 @@ +/* + * 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.xiaominfo.swagger.service.order.controller; + +import com.google.common.collect.Lists; +import com.xiaominfo.swagger.service.order.common.Rest; +import com.xiaominfo.swagger.service.order.model.Order; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/*** + * + * @since:swagger-bootstrap-ui 1.0 + * @author xiaoymin@foxmail.com + * 2019/05/04 11:26 + */ +@Api(tags = "订单模块") +@RestController +@RequestMapping("/user") +public class OrderController { + + + @ApiOperation(value = "查询订单列表") + @PostMapping(value = "/list") + public Rest> list(){ + Rest> rest=new Rest<>(); + List list= Lists.newArrayList(new Order(),new Order(),new Order(),new Order(),new Order(),new Order()); + rest.setData(list); + return rest; + } + + @ApiOperation(value = "根据订单id查询订单详情") + @GetMapping("/queryById") + public Rest queryById(@RequestParam(value = "id") String id){ + Rest userRest=new Rest<>(); + userRest.setData(new Order()); + + return userRest; + } + + + + + +} diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/model/Order.java b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/model/Order.java new file mode 100644 index 0000000000000000000000000000000000000000..fa037ba49f010e36d7ae49dc7c1844a34ecdf23e --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/java/com/xiaominfo/swagger/service/order/model/Order.java @@ -0,0 +1,53 @@ +/* + * 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.xiaominfo.swagger.service.order.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.text.DecimalFormat; +import java.util.Random; + +/*** + * + * @since:swagger-bootstrap-ui 1.0 + * @author xiaoymin@foxmail.com + * 2019/05/04 11:37 + */ +@ApiModel(value = "订单模块") +public class Order { + + @ApiModelProperty(value = "订单号") + private String orderNo; + + @ApiModelProperty(value = "订单名称") + private String name; + + + public Order() { + DecimalFormat df=new DecimalFormat("00000"); + this.orderNo="TAW"+df.format(new Random().nextInt(1000)); + this.name="订单"+new Random().nextInt(1000); + } + + public String getOrderNo() { + return orderNo; + } + + public void setOrderNo(String orderNo) { + this.orderNo = orderNo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-order/src/main/resources/application.yml b/swagger-bootstrap-ui-gateway/service-order/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..8a42ede23a2a1e48f2d0170504f10a4db28ffee3 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + port: 10002 +spring: + application: + name: service-order +eureka: + client: + serviceUrl: + defaultZone: http://localhost:10000/eureka/ diff --git a/swagger-bootstrap-ui-gateway/service-order/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java b/swagger-bootstrap-ui-gateway/service-order/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..f8ee544014887b4f1cf45408eb796da37c1f12d3 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-order/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java @@ -0,0 +1,16 @@ +package com.xiaominfo.swagger.service.order; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ServiceUserApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-server/pom.xml b/swagger-bootstrap-ui-gateway/service-server/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..467b557279ed775a3d1040f74d5d1cceba44d02e --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-server/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.xiaominfo.swagger + swagger-bootstrap-ui-gateway + 1.0 + ../pom.xml + + com.xiaominfo.swagger + service-server + service-server + SwaggerBootstrapUi eureka Server Project + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/swagger-bootstrap-ui-gateway/service-server/src/main/java/com/xiaominfo/swagger/service/ServiceServerApplication.java b/swagger-bootstrap-ui-gateway/service-server/src/main/java/com/xiaominfo/swagger/service/ServiceServerApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..b67bc125feb93f736d9e4ee5b47a9ad38ccf0fd8 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-server/src/main/java/com/xiaominfo/swagger/service/ServiceServerApplication.java @@ -0,0 +1,34 @@ +package com.xiaominfo.swagger.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.Environment; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +@EnableEurekaServer +@SpringBootApplication +public class ServiceServerApplication { + + static Logger logger= LoggerFactory.getLogger(ServiceServerApplication.class); + + public static void main(String[] args) throws UnknownHostException { + ConfigurableApplicationContext application=SpringApplication.run(ServiceServerApplication.class, args); + Environment env = application.getEnvironment(); + logger.info("\n----------------------------------------------------------\n\t" + + "Application '{}' is running! Access URLs:\n\t" + + "Local: \t\thttp://localhost:{}\n\t" + + "External: \thttp://{}:{}\n\t"+ + "----------------------------------------------------------", + env.getProperty("spring.application.name"), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port")); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-server/src/main/resources/application.yml b/swagger-bootstrap-ui-gateway/service-server/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..3050e7026b66450759caa3e0066cea4d06ea9cd0 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-server/src/main/resources/application.yml @@ -0,0 +1,13 @@ +server: + port: 10000 +eureka: + instance: + hostname: localhost + client: + register-with-eureka: false + fetch-registry: false + service-url: + defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ +spring: + application: + name: swagger-bootstrap-ui-server \ No newline at end of file diff --git a/swagger-bootstrap-ui-gateway/service-server/src/test/java/com/xiaominfo/swagger/service/ServiceServerApplicationTests.java b/swagger-bootstrap-ui-gateway/service-server/src/test/java/com/xiaominfo/swagger/service/ServiceServerApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..220e820893f213cb35043fa3eee4586e610d7d65 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-server/src/test/java/com/xiaominfo/swagger/service/ServiceServerApplicationTests.java @@ -0,0 +1,16 @@ +package com.xiaominfo.swagger.service; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ServiceServerApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-user/pom.xml b/swagger-bootstrap-ui-gateway/service-user/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..639bda56732823cc3d134ae2c1819cb74e338072 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.xiaominfo.swagger + swagger-bootstrap-ui-gateway + 1.0 + ../pom.xml + + service-user + service-user + User Service + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/ServiceUserApplication.java b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/ServiceUserApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..9cc1c23fb119a39a91c34b02cd8ca1fdb47d5a0e --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/ServiceUserApplication.java @@ -0,0 +1,38 @@ +package com.xiaominfo.swagger.service.user; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.Environment; + +import java.net.InetAddress; +import java.net.UnknownHostException; + + +@EnableEurekaClient +@SpringBootApplication +public class ServiceUserApplication { + + static Logger logger= LoggerFactory.getLogger(ServiceUserApplication.class); + + public static void main(String[] args) throws UnknownHostException { + ConfigurableApplicationContext application=SpringApplication.run(ServiceUserApplication.class, args); + Environment env = application.getEnvironment(); + logger.info("\n----------------------------------------------------------\n\t" + + "Application '{}' is running! Access URLs:\n\t" + + "Local: \t\thttp://localhost:{}\n\t" + + "External: \thttp://{}:{}\n\t"+ + "Doc: \thttp://{}:{}/doc.html\n"+ + "----------------------------------------------------------", + env.getProperty("spring.application.name"), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port")); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/common/Rest.java b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/common/Rest.java new file mode 100644 index 0000000000000000000000000000000000000000..c892190862f8b1367894ec546755ac623cd1be65 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/common/Rest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 Zhejiang BYCDAO Technology CO.,LTD. + * All rights reserved. + * Official Web Site: http://www.bycdao.com. + * Developer Web Site: http://open.bycdao.com. + */ + +package com.xiaominfo.swagger.service.user.common; + +import io.swagger.annotations.ApiModelProperty; + +/*** + * + * @since:swagger-bootstrap-ui-demo 1.0 + * @author xiaoymin@foxmail.com + * 2017/08/21 16:41 + */ +public class Rest { + + @ApiModelProperty(value = "是否成功") + private boolean success=true; + @ApiModelProperty(value = "返回对象") + private T data; + @ApiModelProperty(value = "错误编号") + private Integer errCode; + @ApiModelProperty(value = "错误信息") + private String message; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public Integer getErrCode() { + return errCode; + } + + public void setErrCode(Integer errCode) { + this.errCode = errCode; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/config/SwaggerConfiguration.java b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/config/SwaggerConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..0aee0dc45a1db83e5b9d07f8149aa7f78f6e7e7e --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/config/SwaggerConfiguration.java @@ -0,0 +1,86 @@ + +package com.xiaominfo.swagger.service.user.config; + +import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; +import com.google.common.collect.Lists; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.annotation.Order; +import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +import java.util.List; + + +@Configuration +@EnableSwagger2 +@EnableSwaggerBootstrapUI +@Import(BeanValidatorPluginsConfiguration.class) +public class SwaggerConfiguration { + + @Bean(value = "userApi") + @Order(value = 1) + public Docket groupRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(groupApiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.xiaominfo.swagger.service.user.controller")) + .paths(PathSelectors.any()) + + .build().securityContexts(Lists.newArrayList(securityContext(),securityContext1())).securitySchemes(Lists.newArrayList(apiKey(),apiKey1())); + } + + private ApiInfo groupApiInfo(){ + return new ApiInfoBuilder() + .title("swagger-bootstrap-ui很棒~~~!!!") + .description("
swagger-bootstrap-ui-demo RESTful APIs
") + .termsOfServiceUrl("http://www.group.com/") + .contact("group@qq.com") + .version("1.0") + .build(); + } + + + + private ApiKey apiKey() { + return new ApiKey("BearerToken", "Authorization", "header"); + } + private ApiKey apiKey1() { + return new ApiKey("BearerToken1", "Authorization-x", "header"); + } + + private SecurityContext securityContext() { + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .forPaths(PathSelectors.regex("/.*")) + .build(); + } + private SecurityContext securityContext1() { + return SecurityContext.builder() + .securityReferences(defaultAuth1()) + .forPaths(PathSelectors.regex("/.*")) + .build(); + } + + List defaultAuth() { + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes)); + } + List defaultAuth1() { + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + return Lists.newArrayList(new SecurityReference("BearerToken1", authorizationScopes)); + } + +} diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/controller/UserController.java b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/controller/UserController.java new file mode 100644 index 0000000000000000000000000000000000000000..55a3bf16212af2f9f4b12b75ab7df19c9727b03d --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/controller/UserController.java @@ -0,0 +1,57 @@ +/* + * 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.xiaominfo.swagger.service.user.controller; + +import com.google.common.collect.Lists; +import com.xiaominfo.swagger.service.user.common.Rest; +import com.xiaominfo.swagger.service.user.model.User; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/*** + * + * @since:swagger-bootstrap-ui 1.0 + * @author xiaoymin@foxmail.com + * 2019/05/04 11:26 + */ +@Api(tags = "用户模块") +@RestController +@RequestMapping("/user") +public class UserController { + + + @ApiOperation(value = "查询用户列表") + @PostMapping(value = "/list") + public Rest> list(){ + Rest> rest=new Rest<>(); + List list= Lists.newArrayList(new User("user1","Java开发工程师","公司2") + ,new User("user2","C开发工程师","公司1") + ,new User("user3","JavaScript工程师","公司3") + ,new User("user4","Ui工程师","公司4") + ,new User("user5","总经理","公司1")); + rest.setData(list); + return rest; + } + + @ApiOperation(value = "根据用户id查询用户详情") + @GetMapping("/queryById") + public Rest queryById(@RequestParam(value = "id") String id){ + Rest userRest=new Rest<>(); + userRest.setData(new User("user5","总经理","公司1")); + + return userRest; + } + + + + + +} diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/model/User.java b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/model/User.java new file mode 100644 index 0000000000000000000000000000000000000000..73746176af7c6ae345ef0c322090f99f5bbebbac --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/java/com/xiaominfo/swagger/service/user/model/User.java @@ -0,0 +1,74 @@ +/* + * 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.xiaominfo.swagger.service.user.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.Random; + +/*** + * + * @since:swagger-bootstrap-ui 1.0 + * @author xiaoymin@foxmail.com + * 2019/05/04 11:26 + */ +@ApiModel(value = "用户") +public class User { + + @ApiModelProperty(value = "姓名") + private String name; + + @ApiModelProperty(value = "年龄") + private Integer age; + + @ApiModelProperty(value = "工作") + private String worker; + + @ApiModelProperty(value = "单位") + private String company; + + public User(String name, String worker, String company) { + this.name = name; + this.worker = worker; + this.company = company; + this.age=new Random().nextInt(100); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getWorker() { + return worker; + } + + public void setWorker(String worker) { + this.worker = worker; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } +} diff --git a/swagger-bootstrap-ui-gateway/service-user/src/main/resources/application.yml b/swagger-bootstrap-ui-gateway/service-user/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..ac5f6f7deb73e99412eb460ed7f461864754dc21 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + port: 10001 +spring: + application: + name: service-user +eureka: + client: + serviceUrl: + defaultZone: http://localhost:10000/eureka/ diff --git a/swagger-bootstrap-ui-gateway/service-user/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java b/swagger-bootstrap-ui-gateway/service-user/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..f8ee544014887b4f1cf45408eb796da37c1f12d3 --- /dev/null +++ b/swagger-bootstrap-ui-gateway/service-user/src/test/java/com/xiaominfo/swagger/service/order/ServiceUserApplicationTests.java @@ -0,0 +1,16 @@ +package com.xiaominfo.swagger.service.order; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ServiceUserApplicationTests { + + @Test + public void contextLoads() { + } + +}