Spring Boot引入swagger-ui 后swagger-ui.html無(wú)法訪問(wèn)404的問(wèn)題
最近給graphserver增加swagger,記錄下過(guò)程與問(wèn)題解決。
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù),后端集成下Swagger,然后就可以提供一個(gè)在線文檔地址給前端同學(xué)。
引入 Swagger
pom中加入相關(guān)配置:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
增加Swagger2Config, 添加@EnableSwagger2,可以通過(guò)定義Docket bean實(shí)現(xiàn)自定義。
@Configuration@EnableSwagger2@Profile('swagger')@ComponentScan('xxx.controller')public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(true) .select() .apis(RequestHandlerSelectors.basePackage('xxx.controller')) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title('XXX Rest Server') .description('XXXRest接口') .contact(new Contact('contract', 'url', 'email')) .version('1.0') .build(); }}
swagger-ui.html 404問(wèn)題
項(xiàng)目中有web配置,因此懷疑是這些配置影響了,搜索下發(fā)現(xiàn)這位仁兄有類似經(jīng)歷:https://www.cnblogs.com/pangguoming/p/10551895.html
于是在WebMvcConfig 配置中,override addResourceHandlers
@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler('/**').addResourceLocations('classpath:/static/'); registry.addResourceHandler('swagger-ui.html') .addResourceLocations('classpath:/META-INF/resources/'); registry.addResourceHandler('/webjars/**') .addResourceLocations('classpath:/META-INF/resources/webjars/'); }
搞定收工。
延伸閱讀
server端有了swagger,前端如何更優(yōu)先的調(diào)用?
參見(jiàn):Vue 使用typescript, 優(yōu)雅的調(diào)用swagger API,筆者提供了一個(gè)開(kāi)源npm庫(kù),可以為前端生成調(diào)用axios調(diào)用代碼。
參考 https://www.jb51.net/article/130207.htm
總結(jié)
到此這篇關(guān)于Spring Boot引入swagger-ui 后swagger-ui.html無(wú)法訪問(wèn)404的問(wèn)題的文章就介紹到這了,更多相關(guān)Spring Boot引入 swagger-ui.html無(wú)法訪問(wèn)404內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題2. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)3. Springboot 全局日期格式化處理的實(shí)現(xiàn)4. idea配置jdk的操作方法5. Docker容器如何更新打包并上傳到阿里云6. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法7. VMware中如何安裝Ubuntu8. python 浮點(diǎn)數(shù)四舍五入需要注意的地方9. JAMon(Java Application Monitor)備忘記10. vue實(shí)現(xiàn)web在線聊天功能
