2017-09-07 83 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); 
    } 
} 
相关问题