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

SpringBoot中GetMapping接口始终返回404错误,请求排查

SpringBoot邮箱验证接口GetMapping返回404排查方案

我开发了一个SpringBoot后端API邮箱验证系统,POST /api/send-verification-email接口运行正常,但GET /api/verify-email始终返回404状态码,无法定位原因。以下是我的代码:

@RestController
@RequestMapping("/api")
public class EmailVerificationEndpoint {

    private final JavaMailSender javaMailSender;
    private final EmailProperties emailProperties;
    private final UserRepository userRepository; // Autowire the UserRepository

    @Autowired
    public EmailVerificationEndpoint(JavaMailSender javaMailSender, EmailProperties emailProperties, UserRepository userRepository) {
        this.javaMailSender = javaMailSender;
        this.emailProperties = emailProperties;
        this.userRepository = userRepository;
    }

    @PostMapping("/send-verification-email")
    public ResponseEntity<String> sendVerificationEmail(@RequestBody EmailRequest emailRequest) {
        try {
            // This works fine
        } catch (MessagingException | UnsupportedEncodingException e) {
            // Handle exceptions (e.g., email sending failure) here
            // You can log an error message or return an error response to the frontend
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error sending email");
        }
    }


    @GetMapping("/verify-email")
    public ResponseEntity<String> verifyEmail(@RequestParam String token) {
        try {
            System.out.println("Gets on GET");
            // Look up the user in your database by the provided token
            User user = userRepository.findByVerificationToken(token);

            if (user != null) {
                System.out.println("User found with verification token: " + token);
                // Mark the user as verified in your database
                user.setVerified(true);
                userRepository.save(user);

                // You can also return a success response or redirect to a confirmation page
                return ResponseEntity.ok("Email verified successfully");
            } else {
                // Token is not valid or user doesn't exist
                return ResponseEntity.notFound().build(); // Return a 404 status code
            }
        } catch (Exception e) {
            // Handle exceptions here
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
        }
    }

}

排查方向:

  • 确认请求URL正确性
    检查完整请求地址是否为http://<域名/IP>:<端口>/api/verify-email?token=xxx,注意端口是否匹配SpringBoot启动端口,是否存在额外的上下文路径(如server.servlet.context-path配置),同时避免路径拼写错误(比如大小写、连字符遗漏)。

  • 检查组件扫描与请求映射注册
    确保EmailVerificationEndpoint所在包被SpringBoot组件扫描覆盖,若主启动类包范围小于控制器包,需添加@ComponentScan(basePackages = "xxx")指定扫描路径。启动时查看控制台日志,确认是否有GET /api/verify-email的映射记录,无记录则说明控制器未被正确加载。

  • 验证请求参数匹配
    接口要求携带token请求参数(参数名大小写敏感),测试时需确保URL后拼接?token=你的验证令牌,若参数名不符(如写成code或Token),Spring无法匹配到对应方法,会返回404。

  • 排查拦截器/过滤器影响
    若启用Spring Security,需确认/api/verify-email路径被允许匿名访问,避免被拦截。同时检查全局异常处理器(@ControllerAdvice)或自定义过滤器是否修改了响应状态码,导致请求被提前拦截。

  • 检查路径匹配策略配置
    若application.yml中配置了spring.mvc.pathmatch.matching-strategy=PATH_PATTERN_PARSER,部分含特殊字符的令牌可能导致匹配失败,可尝试改为ANT_PATH_MATCHER测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 16:15:03