WebMvcConfigurationSupport没有生效的问题

4 篇文章 0 订阅
1 篇文章 0 订阅

问题:

程序中想使用swagger2看看接口是否都正常,但是,输入http://localhost:8080/swagger-ui.html总是提示404。而通过postman来请求,都是可以的,推测swagger配置有问题。

 

前端页面错误如下(提示找不到页面):

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

Thu Jun 20 17:51:17 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

后端错误如下(找不到“/swagger-ui.html”对应的“处理器”):

Did not find handler method for [/swagger-ui.html]

疑惑:这个应该在swagger-ui包的resources目录下,是已经存在的,为什么找不到?

 

解决:

1. 初次尝试

找到了可行的方法:

@Configuration
public class SwaggerHandler extends WebMvcConfigurationSupport {

    /**
     * 页面中需要的资源添加资源处理器进行特殊处理
     */
    @Override
    protected 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/");
        super.addResourceHandlers(registry);
    }

    /**
     * 配置默认servlet处理
     */
     @Override
     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
         configurer.enable();
     }
}

该类的目的,就是添加“资源处理器”来处理swagger页面相关的资源。仍旧提示原来的错误。

 

2.关于WebMvcConfigurationSupport第二次尝试

在以上SwaggerHandler类中的addResourceHandlers中打了断点,发现初始化时并没有调用该方法。

问题找到了,该方法没有执行,可是为什么没有执行呢!

经过几次试错,突然想起来可能存在多个WebMvcConfigurationSupport的子类,会不会是多个子类导致的(另一个WebMvcConfigurationSupport的子类用于将不存在的链接转化为json格式返回)。

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    /**
     * 移动设备,不存在链接的拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MobileRequestInterceptor())
                .addPathPatterns("/dev/**");
        super.addInterceptors(registry);
    }
}

根据这个猜想,将两个类合成一个(其实本身就应该写到一起):

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 移动设备,不存在链接的拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MobileRequestInterceptor())
                .addPathPatterns("/dev/**");
        super.addInterceptors(registry);
    }

    /**
     * 页面中需要的资源添加资源处理器进行特殊处理
     */
    @Override
    protected 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/");
        super.addResourceHandlers(registry);
    }

    /**
     * 配置默认servlet处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

问题解决。

 

总结

在一个项目中WebMvcConfigurationSupport只能存在一个,多个的时候,只有一个会生效。以后有机会再看看源码是怎么回事。

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值