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

Spring Boot资源服务器集成spring-addons-starter-oidc遇404错误

问题分析与解决方案

核心原因

你的SimpleRestService控制器未被Spring Boot自动扫描并注册到应用上下文,导致DispatcherServlet无法匹配/service1/hello的请求处理方法,最终返回404。

从请求日志可以明确:Spring Security已完成认证(日志显示Authenticated token、Set SecurityContextHolder),且请求通过了安全过滤器链(Secured GET /service1/hello),之后才触发/error请求,说明安全配置无问题,问题出在Spring MVC请求映射环节。

排查与修复步骤

  • 检查主类是否添加@SpringBootApplication注解
    该注解是Spring Boot应用的核心,包含组件扫描、自动配置等关键功能。如果主类Service1缺少此注解,Spring无法自动发现控制器类。正确的主类代码应如下:

    @SpringBootApplication
    public class Service1 {
        public static void main(String[] args) {
            new SpringApplicationBuilder(Service1.class).web(WebApplicationType.SERVLET).run(args);
        }
    }
    
  • 确认控制器包路径是否符合扫描规则
    Spring Boot默认扫描主类所在包及其所有子包中的组件。如果SimpleRestService所在包与主类Service1的包无父子关系,需在@SpringBootApplication中手动指定扫描路径:

    @SpringBootApplication(scanBasePackages = {"你的主类包路径", "控制器所在包路径"})
    
  • 验证控制器注册状态
    启动应用时,查看日志中是否存在类似Mapped "{[/service1/hello],methods=[GET]}"的条目。若没有,说明控制器未被成功注册,需重新检查上述两点配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 18:57:04