1

I am using this annotation within a Controller's method in one Spring Boot app.

@RequestMapping(value="/{x}/{y}/{filename:.*}", method = RequestMethod.GET)

All is working good and the last parameter can be any filename.

The problem is with urls where that filename ends with ".ico"...Spring is not sending the request to this method...my guess it is that it thinks a favicon itself.

How can I avoid this kind of conflict?

Thanks.

Antonio Acevedo
  • 1,480
  • 3
  • 21
  • 39

2 Answers2

1

Have a look at Spring MVC @PathVariable with dot (.) is getting truncated, especially one of the latest answers regarding Spring 4.x

Community
  • 1
  • 1
Bertrand Renuart
  • 1,608
  • 17
  • 24
  • Hi Bertrand, I tried with the solutions provided in that post but it seems to me that there is a "high-level" rule for favicons ("ico" files), as it is working fine with other kind of image suffixes like "png" or "jpg". Thanks anyway, will give it another try :) – Antonio Acevedo Jun 04 '15 at 07:36
0

I found the solution. I just need to disable this setting inside the application.properties file

spring.mvc.favicon.enabled=false

This way the FaviconConfiguration bean from WebMvcAutoConfiguration does not satisfies the constraint, thus is not created:

@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Bean
    public SimpleUrlHandlerMapping faviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(Integer.MIN_VALUE + 1);
        /**THIS WAS THE CONFLICTIVE MAPPING IN MY CASE**/
        mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
        return mapping;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Bean
    public ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
        requestHandler.setLocations(getLocations());
        return requestHandler;
    }

    private List<Resource> getLocations() {
        List<Resource> locations = new ArrayList<Resource>(CLASSPATH_RESOURCE_LOCATIONS.length + 1);
        for (String location : CLASSPATH_RESOURCE_LOCATIONS) {
            locations.add(this.resourceLoader.getResource(location));
        }
        locations.add(new ClassPathResource("/"));
        return Collections.unmodifiableList(locations);
    }
}

Source: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

Antonio Acevedo
  • 1,480
  • 3
  • 21
  • 39