fix:配置swagger

上级 6e27ca4c
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mapstruct.version>1.4.1.Final</mapstruct.version> <mapstruct.version>1.4.1.Final</mapstruct.version>
<swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version>
<swagger.version>2.9.2</swagger.version>
<swagger-models.version>1.5.24</swagger-models.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
...@@ -102,17 +105,6 @@ ...@@ -102,17 +105,6 @@
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
</dependency> </dependency>
<!--集成swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
...@@ -140,13 +132,11 @@ ...@@ -140,13 +132,11 @@
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version> <version>3.5.1</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/service -->
<dependency> <dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId> <groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId> <artifactId>service</artifactId>
<version>0.11.0</version> <version>0.11.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/client -->
<dependency> <dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId> <groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>client</artifactId> <artifactId>client</artifactId>
...@@ -175,7 +165,6 @@ ...@@ -175,7 +165,6 @@
<version>4.12</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
...@@ -192,6 +181,36 @@ ...@@ -192,6 +181,36 @@
<artifactId>hanlp</artifactId> <artifactId>hanlp</artifactId>
<version>portable-1.8.3</version> <version>portable-1.8.3</version>
</dependency> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-models.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger-models.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -2,18 +2,18 @@ package com.kwan.springbootkwan; ...@@ -2,18 +2,18 @@ package com.kwan.springbootkwan;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@EnableScheduling @EnableScheduling
@SpringBootApplication
@EnableBatchProcessing @EnableBatchProcessing
@SpringBootApplication
public class SpringBootKwanApplication { public class SpringBootKwanApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootKwanApplication.class, args); SpringApplication.run(SpringBootKwanApplication.class, args);
System.out.println("------------------SpringBootKwan应用启动成功--------------------"); System.out.println("------------------SpringBootKwan应用启动成功--------------------");
} }
} }
package com.kwan.springbootkwan.config; package com.kwan.springbootkwan.config;
import org.springframework.boot.validation.MessageInterpolatorFactory; import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
...@@ -14,7 +13,17 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; ...@@ -14,7 +13,17 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver; 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.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
...@@ -29,6 +38,21 @@ import java.util.List; ...@@ -29,6 +38,21 @@ import java.util.List;
@Configuration @Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvcConfigurer { 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 @Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// 解决controller返回字符串中文乱码问题 // 解决controller返回字符串中文乱码问题
...@@ -57,7 +81,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc ...@@ -57,7 +81,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc
messageSource.setDefaultEncoding("utf-8");// 读取配置文件的编码格式 messageSource.setDefaultEncoding("utf-8");// 读取配置文件的编码格式
messageSource.setCacheMillis(-1);// 缓存时间,-1表示不过期 messageSource.setCacheMillis(-1);// 缓存时间,-1表示不过期
messageSource.setBasename("ValidationMessages");// 配置文件前缀名,设置为Messages,那你的配置文件必须以Messages.properties/Message_en.properties... messageSource.setBasename("ValidationMessages");// 配置文件前缀名,设置为Messages,那你的配置文件必须以Messages.properties/Message_en.properties...
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(); MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
factoryBean.setMessageInterpolator(interpolatorFactory.getObject()); factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
...@@ -70,17 +93,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc ...@@ -70,17 +93,6 @@ public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvc
return WebMvcConfigurer.super.getMessageCodesResolver(); return WebMvcConfigurer.super.getMessageCodesResolver();
} }
/**
* 配置静态资源
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
@Override @Override
public void configurePathMatch(PathMatchConfigurer configurer) { public void configurePathMatch(PathMatchConfigurer configurer) {
WebMvcConfigurer.super.configurePathMatch(configurer); WebMvcConfigurer.super.configurePathMatch(configurer);
......
...@@ -2,6 +2,8 @@ package com.kwan.springbootkwan.config; ...@@ -2,6 +2,8 @@ package com.kwan.springbootkwan.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
...@@ -23,9 +25,15 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; ...@@ -23,9 +25,15 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
public class Swagger2Config { public class Swagger2Config {
@Bean @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) return new Docket(DocumentationType.SWAGGER_2)
.groupName("SpringBoot学习") .groupName("SpringBoot学习")
.pathMapping("/")
.enable(flag)
.apiInfo(apiInfo()) .apiInfo(apiInfo())
.select() .select()
.apis(RequestHandlerSelectors.basePackage("com.kwan.springbootkwan.controller"))//需要生成接口文档的包名 .apis(RequestHandlerSelectors.basePackage("com.kwan.springbootkwan.controller"))//需要生成接口文档的包名
......
...@@ -26,9 +26,6 @@ spring: ...@@ -26,9 +26,6 @@ spring:
#seata: #seata:
# tx-service-group: default_tx_group # tx-service-group: default_tx_group
swagger:
enable: true
#seata: #seata:
# application-id: spring-boot-name # application-id: spring-boot-name
# tx-service-group: my-tx-group # tx-service-group: my-tx-group
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册