POM依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
启动类:
@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
@EnableOpenApi
public class UserBasicApplication {
配置类:
@Configuration
//@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.sdj.saas.user_basic.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot Swagger")
.description("用户模块文档")
.version("1.0")
.license("The Apache License")
.build());
}
}
运行产生如下错误:
webMvcConfigure如下:
@Configuration public class WebApiConfig extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /* //访问当前项目的静态资源文件夹下的 js,css,image 等文件 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/"); //SpringMVC 的相关配置都在 WebMvcAutoConfiguration 文件中 //以jar包的方式引入静态资源 registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");*/ //"/swagger-ui/**"为访问路径正则表达式 //"classpath:/META-INF/resources/webjars/springfox-swagger-ui/"为静态资源路径 registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); super.addResourceHandlers(registry); } /* @Override protected void addViewControllers(ViewControllerRegistry registry) { registry.addViewController( "/swagger-ui/") .setViewName("forward:" + "/swagger-ui/index.html"); super.addViewControllers(registry); }*/ }
具体的异常信息:
java.lang.NullPointerException: null
at springfox.documentation.swagger2.mappers.RequestParameterMapper.bodyParameter(RequestParameterMapper.java:264) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.RequestParameterMapper.mapParameter(RequestParameterMapper.java:149) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper.beforeMappingOperations(ServiceModelToSwagger2Mapper.java:125) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapOperation(ServiceModelToSwagger2MapperImpl.java:109) ~[springfox-swagger2-3.0
给的信息不全,从错误提示来看,应该是静态资源被拦截掉了。
找不到对应的静态文件
类似的大概原因:项目中使用了Spring Security,swagger2相关的资源没做免登录配置
webMvcConfigure如下:
@Configuration public class WebApiConfig extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /* //访问当前项目的静态资源文件夹下的 js,css,image 等文件 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/"); //SpringMVC 的相关配置都在 WebMvcAutoConfiguration 文件中 //以jar包的方式引入静态资源 registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");*/ //"/swagger-ui/**"为访问路径正则表达式 //"classpath:/META-INF/resources/webjars/springfox-swagger-ui/"为静态资源路径 registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); super.addResourceHandlers(registry); } /* @Override protected void addViewControllers(ViewControllerRegistry registry) { registry.addViewController( "/swagger-ui/") .setViewName("forward:" + "/swagger-ui/index.html"); super.addViewControllers(registry); }*/ }
具体的异常信息:
java.lang.NullPointerException: null
at springfox.documentation.swagger2.mappers.RequestParameterMapper.bodyParameter(RequestParameterMapper.java:264) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.RequestParameterMapper.mapParameter(RequestParameterMapper.java:149) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper.beforeMappingOperations(ServiceModelToSwagger2Mapper.java:125) ~[springfox-swagger2-3.0.0.jar!/:3.0.0]
at springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapOperation(ServiceModelToSwagger2MapperImpl.java:109) ~[springfox-swagger2-3.0
package com.sdj.saas.user_basic.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
private final String baseUrl;
public SwaggerUiWebMvcConfigurer() {
this.baseUrl = "http://localhost:10010";
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
registry.
addResourceHandler(baseUrl + "/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(baseUrl + "/swagger-ui/")
.setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/pet")
.allowedOrigins("http://editor.swagger.io");
registry
.addMapping("/v2/api-docs.*")
.allowedOrigins("http://editor.swagger.io");
}
}
您好,我是问答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题。
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~