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

Spring RestController无法响应请求,浏览器返回白标错误

Spring Boot RestController 白标错误排查与解决

问题场景

学习Spring Boot时,实现了一个RestController示例,但通过浏览器请求时出现白标错误。已设置应用上下文路径为/learn,相关源码、项目结构及配置如下:

TestController.java

package com.practice.learn.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/learn")
public class TestController {

    TestController(){
        System.out.println("TestController initialized");
    }
    @GetMapping("/test")
    public String testMethod() {
        System.out.println("Inside test method");
        return "This is sample text";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.practice</groupId>
    <artifactId>learn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learn</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

LearnApplication.java

package com.practice.learn;

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

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

application.properties

server.port=8081
server.servlet.context-path=/learn

问题原因及解决方案

核心问题:请求路径重复叠加

在application.properties中设置了server.servlet.context-path=/learn,同时又在TestController类上添加了@RequestMapping("/learn"),导致最终的请求路径变成/learn/learn/test,而非预期的/learn/test,请求路径无对应映射,因此返回白标错误。

解决步骤

  1. 调整控制器注解:移除TestController类上的@RequestMapping("/learn"),控制器路径会自动继承上下文路径:
@RestController
// 移除类级别的/learn映射
public class TestController {

    TestController(){
        System.out.println("TestController initialized");
    }
    @GetMapping("/test")
    public String testMethod() {
        System.out.println("Inside test method");
        return "This is sample text";
    }
}
  1. 验证请求路径:重启应用后,访问http://localhost:8081/learn/test,即可正常返回预期文本。

额外验证点

  • 启动应用时查看控制台,确认是否打印TestController initialized,验证控制器是否被Spring正确扫描初始化。
  • 确认LearnApplication所在的com.practice.learn包包含控制器所在的com.practice.learn.controller子包(默认@SpringBootApplication会扫描当前包及其子包,当前结构符合要求)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 16:13:36