From ae4284f189aac9579eaba0325980405757747cef Mon Sep 17 00:00:00 2001 From: leojames Date: Mon, 25 Dec 2023 17:04:24 +0800 Subject: [PATCH] fix swagger compatibility with springboot --- eladmin-system/pom.xml | 4 ++ .../src/main/java/me/zhengjie/AppRun.java | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/eladmin-system/pom.xml b/eladmin-system/pom.xml index 9119bc126..ac24cf7ba 100644 --- a/eladmin-system/pom.xml +++ b/eladmin-system/pom.xml @@ -86,6 +86,10 @@ oshi-core 6.1.4 + + org.springframework.boot + spring-boot-starter-actuator + diff --git a/eladmin-system/src/main/java/me/zhengjie/AppRun.java b/eladmin-system/src/main/java/me/zhengjie/AppRun.java index ad126018f..33a799a10 100644 --- a/eladmin-system/src/main/java/me/zhengjie/AppRun.java +++ b/eladmin-system/src/main/java/me/zhengjie/AppRun.java @@ -19,14 +19,32 @@ import me.zhengjie.annotation.rest.AnonymousGetMapping; import me.zhengjie.utils.SpringContextHolder; import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties; +import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; +import org.springframework.boot.actuate.endpoint.ExposableEndpoint; +import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver; +import org.springframework.boot.actuate.endpoint.web.EndpointMapping; +import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes; +import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint; +import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier; +import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier; +import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier; +import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.ApplicationPidFileWriter; import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RestController; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + /** * 开启审计功能 -> @EnableJpaAuditing * @@ -63,4 +81,35 @@ public SpringContextHolder springContextHolder() { public String index() { return "Backend service started successfully"; } + + @Bean + public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping( + WebEndpointsSupplier webEndpointsSupplier, + ServletEndpointsSupplier servletEndpointsSupplier, + ControllerEndpointsSupplier controllerEndpointsSupplier, + EndpointMediaTypes endpointMediaTypes, + CorsEndpointProperties corsProperties, + WebEndpointProperties webEndpointProperties, + Environment environment) { + List> allEndpoints = new ArrayList(); + Collection webEndpoints = webEndpointsSupplier.getEndpoints(); + allEndpoints.addAll(webEndpoints); + allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); + allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); + String basePath = webEndpointProperties.getBasePath(); + EndpointMapping endpointMapping = new EndpointMapping(basePath); + boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping( + webEndpointProperties, environment, basePath); + return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, + endpointMediaTypes, corsProperties.toCorsConfiguration(), + new EndpointLinksResolver(allEndpoints, basePath), + shouldRegisterLinksMapping, null); + } + + private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, + Environment environment, String basePath) { + return webEndpointProperties.getDiscovery().isEnabled() && + (StringUtils.hasText(basePath) || + ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); + } }