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

Spring Boot项目访问/registration路径提示资源不可用的问题求助

Spring Boot项目访问/registration路径提示资源不可用的问题求助

我来帮你一步步排查这个404问题,结合你给出的代码和配置,大概率是这几个环节出了问题:

1. 缺少JSP渲染的核心依赖

Spring Boot的web starter默认不包含JSP的嵌入式容器支持,你当前的pom.xml里只有jstl依赖,但还需要添加tomcat-embed-jasper才能让Tomcat正确渲染JSP页面。

在你的<dependencies>里加上这段:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

注意:如果是打包成war包部署,scope用provided;如果是用Spring Boot内嵌Tomcat运行,也可以去掉scope或者设为compile。

2. 确认JSP文件的目录结构是否正确

你的application.properties里配置了:

spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp

这意味着你的registration.jsp必须放在**src/main/webapp/WEB-INF/jsp/**目录下,很多人容易误把JSP放在src/main/resources下,这是不对的,Spring Boot不会从resources目录加载JSP页面。

如果你的项目里没有src/main/webapp目录,手动创建一个(IntelliJ里可以右键项目 -> New -> Directory,命名为webapp)。

3. Spring Security的拦截问题

你引入了spring-boot-starter-security,而Spring Security默认会拦截所有HTTP请求,要求用户认证。如果没有配置允许/registration路径匿名访问,访问时会被拦截,可能表现为404或者自动跳转到登录页。

你需要添加一个Security配置类,允许匿名访问/registration:

package com.boots.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/registration").permitAll() // 允许/registration匿名访问
                .anyRequest().authenticated()
            .and()
            .formLogin()
                .loginPage("/login") // 你配置的登录页
                .permitAll();
    }
}

4. 确认控制器包被Spring Boot扫描到

你的RegistrationController在com.boots.controller包下,要确保你的Spring Boot主启动类(带@SpringBootApplication注解的类)所在的包是com.boots或者更上层的包(比如com),这样Spring才能自动扫描到这个控制器类。

比如主类应该是这样的结构:

package com.boots;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringAppApplication.class, args);
    }
}

最后再试一次

做完以上几步后,重启你的Spring Boot应用,再访问http://localhost:8080/registration,应该就能正常看到注册页面了。

备注:内容来源于stack exchange,提问作者jdiaso

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 11:25:30