问题描述
出现这个问题时,控制台没有任何输出,进程直接退出Process finished with exit code 1
问题解决
尝试加了一行打印语句
System.out.println("SpringBoot Start....");
结果是可以打印出来的:
SpringBoot Start....
Process finished with exit code 1
此时突然想到,那程序入口没问题,就是下一行的问题了。
try {
SpringApplication.run(Application.class, args);
}catch(Exception e) {
e.printStackTrace();
}
结果是依然没有任何输出。但是作为踩坑无数的Java程序员,我们知道Exception还不是最顶级的异常类,于是换成Throwable
try {
SpringApplication.run(Application.class, args);
}catch(Throwable e) {
e.printStackTrace();
}
启动SpringBoot项目的时候控制台输出的log如下:
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
Spring boot集成swagger3出现错误:
原因: 这是因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
解决:
1、在application.properties里配置:spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
2、在SwaggerConfig添加以下bean
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}