2017-09-07 43 views
2

我有一個Spring Boot Rest Web服務。我還將swagger-ui的靜態內容添加到了src/main/resources/static.swagger-ui中,以便我可以訪問我的文檔。ApplicationInsights打破靜態內容的默認Spring Boot配置

默認情況下,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這個文件

... 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 0.6

回答

0

調試了一整天之後,我想我找到了根本原因(解決方法肯定)

根據Spring Boot的documentation它自動配置Spring MVC。這涉及靜態內容的配置。

如果我們看看applicationinsights.web pacakge有一個叫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()); 
    } 
} 

類這是什麼文件說:

如果你想保持彈簧引導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); 
    } 
}