前言:本文旨在解决微服务下通过网关访问所用服务的knife4j文档,无需再通过其他服务单独访问
1.配置类:
在这个文件中注意下basePackage的扫描路径,修改为对应controller下的路径。
- @Configuration
- @EnableSwagger2WebMvc
- public class SwaggerConfiguration {
-
- @Bean
- public Docket getDocket() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(getApiInfoBuilder())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.example.micorder.controller"))
- .paths(PathSelectors.any())
- .build();
- }
-
- private ApiInfo getApiInfoBuilder() {
- return new ApiInfoBuilder()
- .title("Knife4j-mic-order文档")
- .description("swagger-bootstrap-ui-demo RESTful APIs")
- .termsOfServiceUrl("服务url")
- .contact(new Contact("xxx", "url", "xxx@qq.com"))
- .version("1.0")
- .build();
- }
- }
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
-
com.example -
microservices -
0.0.1-SNAPSHOT -
-
-
mic-elasticsearch -
0.0.1-SNAPSHOT -
mic-elasticsearch -
Demo project for Spring Boot -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
-
com.github.xiaoymin -
knife4j-micro-spring-boot-starter -
${knife4j.version} -
-
-
-
com.alibaba.cloud -
spring-cloud-starter-alibaba-nacos-discovery -
-
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
-
3.配置
- # https://doc.xiaominfo.com/knife4j
- knife4j:
- # 开启增强配置
- enable: true
- # 是否开启生产环境屏蔽 true:关闭swagger,false:开启swagger
- production: false
- basic:
- # 是否开启认证
- enable: false
- # Basic认证用户名
- username: admin
- # Basic认证密码
- password: 123456
- spring:
- application:
- name: test-knife4j
- @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;
- }
- }
- @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)));
- }
- }
- server:
- port: 5000
- spring:
- application:
- name: gateway
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- group: microservices
- gateway:
- routes:
- - id: elasticsearch
- uri: lb://elasticsearch
- predicates:
- - Path=/elasticsearch/**
- filters:
- - StripPrefix=1
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
-
com.example -
microservices -
0.0.1-SNAPSHOT -
-
-
mic-gateway -
0.0.1-SNAPSHOT -
mic-gateway -
网关模块 -
-
-
-
com.alibaba.cloud -
spring-cloud-starter-alibaba-nacos-discovery -
-
-
org.springframework.cloud -
spring-cloud-starter-gateway -
-
-
org.springframework.cloud -
spring-cloud-starter-loadbalancer -
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
org.projectlombok -
lombok -
1.18.22 -
provided -
-
-
-
-
com.github.xiaoymin -
knife4j-spring-boot-starter -
3.0.3 -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
-
-
nacos中模块是否在同一分组下以及是否注册。
访问下边的路径即可
http://localhost:5000/doc.html