You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC含点URL资源映射问题:Universal Links配置遇404

解决Spring 4.3.7/Spring Boot 1.4.5中Universal Links的.well-known路径404问题

我之前帮朋友排查过类似的问题,核心原因是Spring MVC默认的路径匹配逻辑会把带点的路径片段当成资源扩展名处理,导致.well-known无法被正确识别为路径的一部分,进而返回404。下面给你两种可靠的解决方案:

方案一:调整Spring MVC路径匹配规则

保持你最初的ResourceHandler配置(带点的路径),同时关闭后缀模式匹配,让Spring把.well-known当作路径节点而非文件扩展名:

  1. 保留正确的资源映射配置:
@Override 
public void addResourceHandlers(final ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/.well-known/**")
            .addResourceLocations("classpath:/well-known/"); 
}
  1. 添加路径匹配器的配置,禁用后缀模式匹配:
@Bean
public PathMatcher pathMatcher() {
    AntPathMatcher pathMatcher = new AntPathMatcher();
    // 关闭后缀匹配,避免把.well-known识别成扩展名
    pathMatcher.setUseSuffixPatternMatch(false);
    return pathMatcher;
}

或者更简单的方式,直接在application.properties里加一行配置:

spring.mvc.pathmatch.use-suffix-pattern=false

方案二:用控制器直接映射目标路径

如果方案一没生效,你可以绕过ResourceHandler的路径匹配限制,写个简单的控制器直接返回文件:

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UniversalLinkController {

    @GetMapping("/.well-known/apple-app-site-association")
    public ResponseEntity<Resource> getAppleAppSiteAssociation() {
        // 指向src/main/resources/well-known目录下的目标文件
        Resource resource = new ClassPathResource("well-known/apple-app-site-association");
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(resource);
    }
}

最后验证

  1. 确认apple-app-site-association文件没有额外扩展名(比如不要写成apple-app-site-association.json),且确实放在src/main/resources/well-known/目录下;
  2. 重启服务器后,访问http://你的服务器地址/.well-known/apple-app-site-association,检查是否能正常返回JSON内容。

内容的提问来源于stack exchange,提问作者dnc253

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:36:54