小編給大家分享一下springboot整合swagger問題的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
解決了一個困擾很久的問題。自己搭建的一個springboot項目,整合完jsp之后可以正常訪問前端界面,但當再整合上swagger之后,前端界面就無法訪問了。當注釋掉swagger之后,前端界面又可以正常訪問。
<!-- 添加jsp引用 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>8.5.31</version> </dependency> <!--引入jquery包--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-2</version> </dependency>
#Spring boot視圖配置 #前綴 spring.mvc.view.prefix=/WEB-INF/ #后綴 spring.mvc.view.suffix=.jsp #靜態文件訪問配置 spring.mvc.static-path-pattern=/static/** spring.thymeleaf.cache=false


<!--swagger相關依賴--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>

package com.xq.config;
import com.xq.common.constants.AppConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
//因為swagger與靜態文件訪問配置沖突,所以整合swagger需要
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
// springboot 集成swagger2.2后靜態資源404,添加如下兩行配置
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("xq")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xq.yexiong.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(AppConstants.app+"的API接口")
.version("1.0")
.build();
}
}
整合完swagger后,首頁無法訪問。注釋掉swagger配置文件后,又可以正常訪問首頁。報錯為:Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'。
查了很多,直到今日。

在啟動類加如下代碼即可。加上該代碼后可以同時訪問首頁與swagger文檔。
@Bean
public InternalResourceViewResolver setupViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
return resolver;
}以上是“springboot整合swagger問題的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。