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

Spring Boot中@RestController配置的端点无法访问,求排查方向

Spring Boot Whitelabel错误排查方案

问题背景

近期学习Spring,搭建了一个多模块Spring Boot应用,仅包含一个带有@GetMapping("/recipe")注解的RestController,但浏览器访问时出现Whitelabel错误页面。

排查方向

  • 组件扫描范围问题:主启动类SlimChefApplication位于com.calculate.calories.start包下,而控制器RecipeController在com.calculate.calories.web.controller包下。默认@SpringBootApplication仅扫描主类所在包及其子包,无法识别同级的web子包。需修改主类注解,指定扫描根包:
    @SpringBootApplication(
        exclude = {DataSourceAutoConfiguration.class},
        scanBasePackages = "com.calculate.calories"
    )
    
  • 多模块依赖有效性:确认config模块(启动模块)是否正确依赖application模块,且Maven编译打包后,config的target目录中包含application模块的控制器class文件。可执行mvn clean install重新构建项目。
  • 访问路径与端口验证:确认应用启动端口(默认8080),访问路径是否为http://localhost:8080/recipe,避免路径拼写错误。
  • 启动日志检查:查看应用启动日志,寻找是否存在控制器映射的日志(如Mapping GET /recipe to ...)。若没有该日志,说明控制器未被Spring容器注册。
  • Web依赖冲突:application模块同时引入了spring-boot-starter-web和spring-boot-starter-webflux,两者分别对应Servlet栈和Reactive栈,可能导致Web容器初始化异常。建议移除其中一个依赖,保留spring-boot-starter-web即可。

相关代码

控制器代码

package com.calculate.calories.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

@RestController
public class RecipeController {

    @GetMapping("/recipe")
    public String getRecipes() {
        HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.aniagotuje.pl/client/posts/search?categories=ciasta-i-torty&diets=&occasions=&tags=&page=0&sort=publish,desc"))
                .GET()
                .timeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();

        HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.of(15, ChronoUnit.SECONDS)).build();
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            return response.body();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return "e";
    }
}

主启动类代码

package com.calculate.calories.start;

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

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SlimChefApplication {

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

主POM文件

<?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>
    <modules>
        <module>config</module>
        <module>persistence</module>
        <module>common</module>
        <module>application</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.calculate.calories</groupId>
    <artifactId>slimchef</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SlimChef</name>
    <description>SlimChef</description>
    <packaging>pom</packaging>

    <properties>
        <java.version>17</java.version>
        <project.version>0.0.1-SNAPSHOT</project.version>
    </properties>
</project>

application模块POM文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>slimchef</artifactId>
        <groupId>com.calculate.calories</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>application</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
</project>

config模块POM文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>slimchef</artifactId>
        <groupId>com.calculate.calories</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.calculate.calories</groupId>
            <artifactId>application</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.calculate.calories</groupId>
            <artifactId>persistence</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:15:43