当前位置:   article > 正文

总结错误 No mapping for GET /swagger-ui.html

no mapping for get /swagger-ui.html

在SpringBoot项目引入Swagger2后,在浏览器地址里输入地址:http://ip:port/swagger-ui.html 报错:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 06 15:48:30 CST 2021

There was an unexpected error (type=Not Found, status=404).

No message available

查看后台控制台,发现这样的错误:

o.s.web.servlet.PageNotFound             : No mapping for GET /swagger-ui.html

经过分析发现由于项目中有配置注解类(@Configuration)继承了WebMvcConfigurationSupport,导致默认的Swagger静态资源被覆盖,而缺失了配置。

可在该继承配置类中,显式添加如下swagger静态资源:

  1. @Override
  2. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  3. registry.addResourceHandler("doc.html")
  4. .addResourceLocations("classpath:/META-INF/resources/");
  5. registry.addResourceHandler("swagger-ui.html")
  6. .addResourceLocations("classpath:/META-INF/resources/");
  7. registry.addResourceHandler("/webjars/**")
  8. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  9. }

即可解决。

其他,不单单swagger,同样的问题,如果因为继承WebMvcConfigurationSupport导致其他默认配置失效。比如,Controller方法上自定义注解失效,则需要在该继承配置类中重写:

  1. @Override
  2. protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  3. argumentResolvers.add(...bean...);
  4. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/99113
推荐阅读
相关标签
  

闽ICP备14008679号