2

I have a Spring Boot Rest Web Service. I've also added static content with swagger-ui to the src/main/resources/static.swagger-ui so I can access my documentation.

By default SimpleUrlHandlerMapping is mapping my request to the static content automatically during startup.

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

I added reference to the Application Insights in my gradle file

... 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}")
...

This is somehow breaking SimpleUrlHandlerMapping as mapping to the static content doesn't work any more.

Any idea how to fix it? I could probably add mapper manually...

My stack:

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

1 Answers1

1

After the whole day of debugging I think I found a root cause (workaround for sure)

According to the documentation of Spring Boot it automaticly configure Spring MVC. That involves configuration of static content.

If we take look at the applicationinsights.web pacakge there is a class called 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());
    }
}

And this is what documentation says:

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc

So it seems that there is a bug in the package as a class contains @EnableWebMvc annotation that breaks auto configuration feature.

I have not recompiled that library without this annotation so I'm not 100% sure if removing this tag would fix this issue. What I did is is adding mapping manually.

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