Spring Boot引入swagger-ui 后swagger-ui.html无法访问404的问题
最近给graphserver增加swagger,记录下过程与问题解决。
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,后端集成下Swagger,然后就可以提供一个在线文档地址给前端同学。
引入Swagger
pom中加入相关配置:
io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
增加Swagger2Config,添加@EnableSwagger2,可以通过定义Docketbean实现自定义。
@Configuration @EnableSwagger2 @Profile("swagger") @ComponentScan("xxx.controller") publicclassSwagger2Config{ @Bean publicDocketcreateRestApi(){ returnnewDocket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(true) .select() .apis(RequestHandlerSelectors.basePackage("xxx.controller")) .paths(PathSelectors.any()) .build(); } privateApiInfoapiInfo(){ returnnewApiInfoBuilder() .title("XXXRestServer") .description("XXXRest接口") .contact(newContact("contract","url","email")) .version("1.0") .build(); } }
swagger-ui.html404问题
项目中有web配置,因此怀疑是这些配置影响了,搜索下发现这位仁兄有类似经历:https://www.cnblogs.com/pangguoming/p/10551895.html
于是在WebMvcConfig配置中,overrideaddResourceHandlers
@Configuration publicclassWebMvcConfigimplementsWebMvcConfigurer{ @Override publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){ 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,前端如何更优先的调用?
参见:Vue使用typescript,优雅的调用swaggerAPI,笔者提供了一个开源npm库,可以为前端生成调用axios调用代码。
参考https://www.nhooo.com/article/130207.htm
总结
到此这篇关于SpringBoot引入swagger-ui后swagger-ui.html无法访问404的问题的文章就介绍到这了,更多相关SpringBoot引入swagger-ui.html无法访问404内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。