From 58ca08bc143cb0cc97f3d36dabbfedfb5fa7d8e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com>
Date: Sat, 26 Aug 2023 14:59:48 +0800
Subject: [PATCH] =?UTF-8?q?fix:=E9=85=8D=E7=BD=AEswagger?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 47 +++++++++++++------
.../SpringBootKwanApplication.java | 6 +--
.../springbootkwan/config/MyWebMvcConfig.java | 42 +++++++++++------
.../springbootkwan/config/Swagger2Config.java | 10 +++-
src/main/resources/application.yaml | 3 --
5 files changed, 72 insertions(+), 36 deletions(-)
diff --git a/pom.xml b/pom.xml
index 4bf3d66..1c38cf5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,6 +19,9 @@
UTF-8
UTF-8
1.4.1.Final
+ 1.9.6
+ 2.9.2
+ 1.5.24
@@ -102,17 +105,6 @@
redis.clients
jedis
-
-
- io.springfox
- springfox-swagger2
- 2.6.1
-
-
- io.springfox
- springfox-swagger-ui
- 2.6.1
-
org.springframework.boot
spring-boot-starter-mail
@@ -140,13 +132,11 @@
dynamic-datasource-spring-boot-starter
3.5.1
-
com.theokanning.openai-gpt3-java
service
0.11.0
-
com.theokanning.openai-gpt3-java
client
@@ -175,7 +165,6 @@
4.12
test
-
org.mapstruct
mapstruct
@@ -192,6 +181,36 @@
hanlp
portable-1.8.3
+
+ io.springfox
+ springfox-swagger2
+ ${swagger.version}
+
+
+ io.swagger
+ swagger-annotations
+
+
+ io.swagger
+ swagger-models
+
+
+
+
+ io.swagger
+ swagger-annotations
+ ${swagger-models.version}
+
+
+ io.swagger
+ swagger-models
+ ${swagger-models.version}
+
+
+ io.springfox
+ springfox-swagger-ui
+ ${swagger.version}
+
diff --git a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java
index 15bcf6e..1aa0afa 100644
--- a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java
+++ b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java
@@ -2,18 +2,18 @@ package com.kwan.springbootkwan;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
+@EnableSwagger2
@EnableScheduling
-@SpringBootApplication
@EnableBatchProcessing
+@SpringBootApplication
public class SpringBootKwanApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootKwanApplication.class, args);
System.out.println("------------------SpringBootKwan应用启动成功--------------------");
}
-
}
diff --git a/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
index 42bc12c..a608e34 100644
--- a/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
+++ b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java
@@ -1,7 +1,6 @@
package com.kwan.springbootkwan.config;
import org.springframework.boot.validation.MessageInterpolatorFactory;
-import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
@@ -14,7 +13,17 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
-import org.springframework.web.servlet.config.annotation.*;
+import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
+import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
+import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.List;
@@ -29,6 +38,21 @@ import java.util.List;
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvcConfigurer {
+ /**
+ * 配置静态资源
+ *
+ * @param registry
+ */
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/static/**")
+ .addResourceLocations("classpath:/static/");
+ registry.addResourceHandler("swagger-ui.html")
+ .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
+ registry.addResourceHandler("/webjars/**")
+ .addResourceLocations("classpath:/META-INF/resources/webjars/");
+ }
+
@Override
public void extendMessageConverters(List> converters) {
// 解决controller返回字符串中文乱码问题
@@ -57,7 +81,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc
messageSource.setDefaultEncoding("utf-8");// 读取配置文件的编码格式
messageSource.setCacheMillis(-1);// 缓存时间,-1表示不过期
messageSource.setBasename("ValidationMessages");// 配置文件前缀名,设置为Messages,那你的配置文件必须以Messages.properties/Message_en.properties...
-
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
@@ -70,17 +93,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc
return WebMvcConfigurer.super.getMessageCodesResolver();
}
- /**
- * 配置静态资源
- *
- * @param registry
- */
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/static/**")
- .addResourceLocations("classpath:/static/");
- }
-
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
WebMvcConfigurer.super.configurePathMatch(configurer);
@@ -156,4 +168,4 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc
public void configureMessageConverters(List> converters) {
WebMvcConfigurer.super.configureMessageConverters(converters);
}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java
index aab803d..0f0e6ca 100644
--- a/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java
+++ b/src/main/java/com/kwan/springbootkwan/config/Swagger2Config.java
@@ -2,6 +2,8 @@ package com.kwan.springbootkwan.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -23,9 +25,15 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
public class Swagger2Config {
@Bean
- public Docket createRestApi() {
+ public Docket createRestApi(Environment environment) {
+ // 配置swagger的docket的bean实例
+ Profiles profiles = Profiles.of("dev", "test", "local");
+ // 通过environment.acceptsProfiles()判断是否指定的环境中,是,则为true
+ boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("SpringBoot学习")
+ .pathMapping("/")
+ .enable(flag)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.kwan.springbootkwan.controller"))//需要生成接口文档的包名
diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml
index 904dd7d..4426832 100644
--- a/src/main/resources/application.yaml
+++ b/src/main/resources/application.yaml
@@ -26,9 +26,6 @@ spring:
#seata:
# tx-service-group: default_tx_group
-swagger:
- enable: true
-
#seata:
# application-id: spring-boot-name
# tx-service-group: my-tx-group
--
GitLab