记录一下spring boot 配置swagger出现404

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lys1695227550/article/details/85014096

1、直接根据网上的配置,一个新建的spring boot 项目配置swagger是可以访问的,但是如果你配置了

 extends WebMvcConfigurationSupport 这样你的自动配置就失败了,我看了很多网上提到的点,就是说

extends WebMvcConfigurationSupport,,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置,有些文章好一点会告诉你位置,而我自己来试着找到该位置,然后试着解决一下这个自动配置失效,自行自定义配置。

我使用的是idea,然后:: Spring Boot ::        (v2.1.1.RELEASE)

1、依赖中找到如下图所示的自动配置的jar

2、找到

3、点击进入

很多项,我可以告诉你搜mvc,你就好找一些

上图你应该在很多文章中可以看到,找到后,你就可以自己分析一下,这边我就不多说了

下面做些测试:

controller测试

静态页面测试:

org.springframework.boot.autoconfigure.web.ResourceProperties中提到了

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

那么我在static中放hello.html

说明我们就是在使用自动配置,现在开始就着前面所说,来取消掉我们自动配置。

现在重启后访问我的lys.html

结果:可以访问,试着访问我的hello.html

结果无法访问,自行接天static路径

所以添加两个是不行的吧,至少不能像我这样。

PS:如果有留心,会发现

1. 在SpringBoot1.X的版本中,我们可以继承自WebMvcConfigurerAdapter,覆盖想要实现的方法即可。

2. 但是在SpringBoot2.X的定义中,WebMvcConfigurerAdapter已经被定义为@Deprecated

被废弃了,但是可以直接实现接口来达到你想要的功能。

下面试着使用网上的方法:使用:

继承 WebMvcConfigurerAdapter保留自动配置,只需要重写方法即可。

现在来试一下

再试一个configureViewResolvers

做些准备

spring boot默认使用thymeleaf 不推荐使用jsp,由于使用比较多的就是jsp,所以整合一下jsp支持

spring boot项目无法新建jsp文件请查看:

https://www.cnblogs.com/sxdcgaq8080/p/7676294.html

spring boot 配置jsp请看:

https://www.cnblogs.com/peterxiao/p/7826427.html

跟着上面的学习,我是配置出来了,如下:

pom文件

<!--整合一下jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!--<scope>provided</scope>-->
</dependency>
<!-- jstl标签库 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

application.properties文件

资源结构(连接中会提)

index.jsp

启动访问:

做上面的所有,只是为了试一下

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {

}
注释配置
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

使用java配置

@Bean
public ViewResolver internalResourceViewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

配置到这就已经实现效果了

@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
    registry.viewResolver(internalResourceViewResolver());
}

为什么还要配置

PS:我觉得就是不加@bean,然后使用上面方式添加,和使用@bean,spring 帮我们添加了,也是未可知。

继续试一下

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/hehe").setViewName("index");
}

效果出来了,但是没有数据,这是必然的,因为我们只是添加了路径与视图,并没有数据处理。

这种用的不多,学习与快速测试时可能用到。

做得这些扩展,回到前面的话题,使用extends WebMvcConfigurationSupport 替换 implements WebMvcConfigurer

public class WxWebMvcConfiguration  extends WebMvcConfigurationSupport {

测试效果是一样的。

回到swagger,发现无法访问404

追根溯源:

而我的配置是

//        swagger 是无法访问的
        registry.addResourceHandler("/**").addResourceLocations("classpath:/lys/");

加上:

registry.addResourceHandler("/**")
        .addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);

即可

以上就是我由这个swagger 404 引发的思考,希望能给大家一下帮助,也为了以后自己能够方便复习。

猜你喜欢

转载自blog.csdn.net/lys1695227550/article/details/85014096
今日推荐