mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 11:07:06 +00:00
(improvement)(project) Fixed the issue of Swagger UI not working. (#1133)
This commit is contained in:
@@ -6,6 +6,7 @@ import com.tencent.supersonic.common.pojo.ResultData;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -24,14 +25,19 @@ public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
|
||||
return true;
|
||||
return !methodParameter.getDeclaringClass().isAssignableFrom(BasicErrorController.class);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object result, MethodParameter methodParameter, MediaType mediaType,
|
||||
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest,
|
||||
ServerHttpResponse serverHttpResponse) {
|
||||
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest,
|
||||
ServerHttpResponse serverHttpResponse) {
|
||||
// 判断当前请求是否是 Swagger 相关的请求
|
||||
String path = serverHttpRequest.getURI().getPath();
|
||||
if (path.startsWith("/swagger") || path.startsWith("/v3/api-docs") || path.startsWith("/v2/api-docs")) {
|
||||
return result;
|
||||
}
|
||||
objectMapper.registerModule(new JavaTimeModule());
|
||||
if (result instanceof String) {
|
||||
return objectMapper.writeValueAsString(ResultData.success(result));
|
||||
|
||||
@@ -70,12 +70,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<artifactId>springfox-boot-starter</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -8,12 +8,14 @@ import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = {"com.tencent.supersonic"},
|
||||
exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
@Import(S2LangChain4jAutoConfiguration.class)
|
||||
@EnableSwagger2
|
||||
public class StandaloneLauncher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.tencent.supersonic.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig {
|
||||
@Bean
|
||||
public Docket api() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.any())
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.tencent.supersonic.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.oas.annotations.EnableOpenApi;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.ApiKey;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
@EnableOpenApi
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
@Value("${swagger.title}")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 基本包
|
||||
*/
|
||||
@Value("${swagger.base.package}")
|
||||
private String basePackage;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
@Value("${swagger.description}")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
@Value("${swagger.url}")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
@Value("${swagger.contact.name}")
|
||||
private String contactName;
|
||||
|
||||
/**
|
||||
* 作者网址
|
||||
*/
|
||||
@Value("${swagger.contact.url}")
|
||||
private String contactUrl;
|
||||
|
||||
/**
|
||||
* 作者邮箱
|
||||
*/
|
||||
@Value("${swagger.contact.email}")
|
||||
private String contactEmail;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
@Value("${swagger.version}")
|
||||
private String version;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationConfig authenticationConfig;
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
|
||||
return new Docket(DocumentationType.OAS_30)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(basePackage))
|
||||
.paths(PathSelectors.any())
|
||||
.build().securitySchemes(Lists.newArrayList(apiKey()));
|
||||
|
||||
}
|
||||
|
||||
private ApiKey apiKey() {
|
||||
return new ApiKey(authenticationConfig.getTokenHttpHeaderKey(),
|
||||
authenticationConfig.getTokenHttpHeaderKey(), "header");
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title(title)
|
||||
.description(description)
|
||||
.termsOfServiceUrl(url)
|
||||
.contact(new Contact(contactName, contactUrl, contactEmail))
|
||||
.version(version)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -102,3 +102,16 @@ s2:
|
||||
# access-token: hg_access_token
|
||||
# model-id: sentence-transformers/all-MiniLM-L6-v2
|
||||
# timeout: 1h
|
||||
|
||||
# swagger配置
|
||||
swagger:
|
||||
title: 'SuperSonic平台接口文档'
|
||||
base:
|
||||
package: com.tencent.supersonic
|
||||
description: 'SuperSonic平台接口文档'
|
||||
url: ''
|
||||
contact:
|
||||
name:
|
||||
email:
|
||||
url: ''
|
||||
version: 3.0
|
||||
|
||||
@@ -75,4 +75,17 @@ s2:
|
||||
#2.embedding-model
|
||||
#2.1 in_memory(default)
|
||||
embedding-model:
|
||||
provider: in_process
|
||||
provider: in_process
|
||||
|
||||
# swagger配置
|
||||
swagger:
|
||||
title: 'SuperSonic平台接口文档'
|
||||
base:
|
||||
package: com.tencent.supersonic
|
||||
description: 'SuperSonic平台接口文档'
|
||||
url: ''
|
||||
contact:
|
||||
name:
|
||||
email:
|
||||
url: ''
|
||||
version: 3.0
|
||||
Reference in New Issue
Block a user