Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在Spring Boot中未检测到以.ico结尾的URL_Spring Mvc_Spring Boot - Fatal编程技术网

Spring mvc 在Spring Boot中未检测到以.ico结尾的URL

Spring mvc 在Spring Boot中未检测到以.ico结尾的URL,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我在一个Spring Boot应用程序的控制器方法中使用此注释 @RequestMapping(value="/{x}/{y}/{filename:.*}", method = RequestMethod.GET) 一切正常,最后一个参数可以是任何文件名 问题是URL的文件名以“.ico”结尾…Spring并没有向这个方法发送请求…我猜是因为它认为favicon本身就是一个favicon 我怎样才能避免这种冲突 谢谢。看一看,特别是关于Spring 4.x的最新答案之一我找到了解决方案。我只需

我在一个Spring Boot应用程序的控制器方法中使用此注释

@RequestMapping(value="/{x}/{y}/{filename:.*}", method = RequestMethod.GET)
一切正常,最后一个参数可以是任何文件名

问题是URL的文件名以“.ico”结尾…Spring并没有向这个方法发送请求…我猜是因为它认为favicon本身就是一个favicon

我怎样才能避免这种冲突


谢谢。

看一看,特别是关于Spring 4.x的最新答案之一

我找到了解决方案。我只需要在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);
    }
}
@配置
@条件属性(value=“spring.mvc.favicon.enabled”,matchIfMissing=true)
公共静态类FaviconConfiguration实现ResourceLoaderware{
私有资源加载器;
@豆子
公共SimpleUrlHandlerMapping faviconHandlerMapping(){
SimpleUrlHandlerMapping mapping=新的SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_值+1);
/**这就是我案例中的冲突映射**/
setUrlMap(Collections.singletonMap(“**/favicon.ico”,faviconRequestHandler());
回归映射;
}
@凌驾
公共void setResourceLoader(ResourceLoader ResourceLoader){
this.resourceLoader=resourceLoader;
}
@豆子
公共资源HttpRequestHandler faviconRequestHandler(){
ResourceHttpRequestHandler requestHandler=新的ResourceHttpRequestHandler();
setLocations(getLocations());
返回请求处理程序;
}
私有列表getLocations(){
列表位置=新数组列表(CLASSPATH\u RESOURCE\u locations.length+1);
for(字符串位置:类路径\资源\位置){
add(this.resourceLoader.getResource(location));
}
添加(新类路径资源(“/”);
返回集合。不可修改列表(位置);
}
}

来源:

Hi Bertrand,我尝试了那篇文章中提供的解决方案,但在我看来,favicons(“ico”文件)有一个“高级”规则,因为它可以很好地处理其他类型的图像后缀,如“png”或“jpg”。无论如何谢谢,我会再试一次:)