【问题标题】:ApplicationInsights breaks default Spring Boot configuration for static contentApplicationInsights 破坏了静态内容的默认 Spring Boot 配置
【发布时间】:2018-02-16 08:31:39
【问题描述】:

我有一个 Spring Boot Rest Web 服务。我还在 src/main/resources/static.swagger-ui 中添加了带有 swagger-ui 的静态内容,因此我可以访问我的文档。

默认情况下,SimpleUrlHandlerMapping 会在启动期间自动将我的请求映射到静态内容。

...
2017-09-07 15:41:21.144  INFO 6912 --- [localhost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-07 15:41:21.144  INFO 6912 --- [localhost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-07 15:41:21.212  INFO 6912 --- [localhost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
...

我在我的 gradle 文件中添加了对 Application Insights 的引用

... part of my gradle file
// Application Insight related jar - required for telemetrics and loggers
compile("com.microsoft.azure:applicationinsights-web:${aiWeb}")
compile("com.microsoft.azure:applicationinsights-logging-logback:${aiLogback}")
...

这在某种程度上破坏了 SimpleUrlHandlerMapping,因为映射到静态内容不再起作用。

知道如何解决吗?我可能可以手动添加映射器...

我的堆栈:

  • springBootVersion=1.5.3.RELEASE
  • springSecurityVersion=4.2.1.RELEASE
  • aiWeb=1.0.9 aiLogback=1.0.6

【问题讨论】:

    标签: spring-mvc spring-boot azure-application-insights


    【解决方案1】:

    经过一整天的调试,我想我找到了根本原因(肯定是解决方法)

    根据Spring Boot的documentation自动配置Spring MVC。这涉及静态内容的配置。

    如果我们查看 applicationinsights.web 包,有一个名为 InterceptorRegistry 的类

    package com.microsoft.applicationinsights.web.spring.internal;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import com.microsoft.applicationinsights.web.spring.RequestNameHandlerInterceptorAdapter;
    
    /**
     * This class registers the RequestNameHandlerInterceptorAdapter to the interceptors registry.
     * The registration enables the interceptor to extract the http request's controller and action names.
     */
    @EnableWebMvc
    @Configuration
    public class InterceptorRegistry extends WebMvcConfigurerAdapter {
    
        @Override
        public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
            registry.addInterceptor(new RequestNameHandlerInterceptorAdapter());
        }
    }
    

    这就是文档所说的:

    如果您想保留 Spring Boot MVC 功能,并且只想添加 额外的 MVC 配置(拦截器、格式化程序、视图 控制器等)您可以添加自己的 @Configuration 类型的类 WebMvcConfigurerAdapter,但没有 @EnableWebMvc

    所以似乎包中有一个错误,因为一个类包含 @EnableWebMvc 注释会破坏自动配置功能。

    我没有重新编译没有这个注释的库,所以我不能 100% 确定删除这个标签是否能解决这个问题。我所做的是手动添加映射。

    @Configuration
    public class MvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
                    .addResourceHandler("/swagger-ui/**")
                    .addResourceLocations("classpath:/static/swagger-ui/");
            super.addResourceHandlers(registry);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-16
      • 2018-02-10
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2020-12-12
      • 2018-06-26
      • 2017-07-19
      相关资源
      最近更新 更多