首页 > 基础资料 博客日记
We‘re sorry but knife4j-vue doesn‘t work properly without JavaScript enabled.
2024-07-09 16:00:06基础资料围观310次
这篇文章介绍了We‘re sorry but knife4j-vue doesn‘t work properly without JavaScript enabled.,分享给大家做个参考,收藏Java资料网收获更多编程知识
springboot集成knife4j出现问题
- We’re sorry but knife4j-vue doesn’t work properly without JavaScript enabled. Please enable it to continue.
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
配置类
package cn.osais.customer.config;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
@EnableOpenApi
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
/**
* 是否开启swagger
*/
@Value("${swagger.enabled:true}")
private boolean enabled;
/**
* 是否开启knife4j
*/
@Value("${knife4j.enabled:true}")
private boolean knife4jEnabled;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.exposedHeaders("*");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 添加资源映射
if (enabled && knife4jEnabled) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("index.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.enable(enabled)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private List<SecurityScheme> securitySchemes() {
List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
return apiKeyList;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(
SecurityContext.builder()
.securityReferences(defaultAuth())
.operationSelector(o -> o.requestMappingPattern().matches("/.*"))
.build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences = new ArrayList<>();
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
return securityReferences;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.description("描述")
.contact(new Contact("作者", null, null))
.version("1.0.0")
.build();
}
}
问题
先说结论,头一天写好配置本地测试完后提交到代码仓库,第二天同事提交了一些拦截器的配置,手动合并代码后就出现这个报错。于是直接在本地拉取新分支,进行版本会退
- 首先把拦截器的注入都去掉,发现还是没用
- 接着又一个类一个类的排查,找到了问题类
问题类
ResponseBodyAdvice.java
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@RestControllerAdvice
public class XXResponseAdvice implements ResponseBodyAdvice<Object> {
...xxx
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// 返回参数进行全局转换
// 1.为空,为数据包装了一层
if (body == null) {
return Result.ok(null);
}
// 2.如果是特定资源,又完整的返回出去
if ("xxx".contains(request.getURI().toString())) {
return body;
}
// 3.根据实例类型做判断返回
if (body instanceof String) {
// 没看懂为什么要data.data包装一层
JSONObject obj = new JSONObject();
obj.add("data", body);
return Result.ok(obj);
} else if (body instanceof Result) {
return body;
} else {
// 问题出在这一句,如果为swagger资源对象直接被包装了,获取不到数据
return Result.ok(body);
}
}
}
文章来源:https://blog.csdn.net/qq_44896851/article/details/138966486
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: