我在一个Spring Boot应用程序的Controller方法中使用了这个注释。
@RequestMapping(value="/{x}/{y}/{filename:.*}", method = RequestMethod.GET)
一切正常,最后一个参数可以是任何文件名。
问题在于url,其中文件名以“.ico”结尾... Spring没有向此方法发送请求...我的猜测是它认为是一个favicon本身。
我怎样才能避免这种冲突?
感谢。
答案 0 :(得分:1)
查看Spring MVC @PathVariable with dot (.) is getting truncated,特别是有关Spring 4.x
的最新答案之一答案 1 :(得分:0)
我找到了解决方案。我只需要在application.properties文件中禁用此设置
spring.mvc.favicon.enabled=false
这样来自WebMvcAutoConfiguration的FaviconConfiguration bean不满足约束,因此不会创建:
@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);
}
}