java - 为什么我从 hello world SpringBoot 收到 WhiteLabel 错误?

标签 java spring spring-mvc spring-boot tomcat

嗨,我正在学习 Spring,我不明白为什么我会收到白标签错误

主类:

package com.hello.springhelloworld;

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

@SpringBootApplication
public class SpringHelloWorldApplication {

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

}

Hello World :

package web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorldController {

@RequestMapping("/hello")
public String sayHello(@RequestParam(value = "name") String name){
    return  "Hello" + name + "!";
}
}

项目结构: enter image description here

错误: enter image description here

最佳答案

Spring Boot 的组件扫描不是在扫描您的 Controller 类。

您的 Controller 类应该与您的 SpringHelloWorldApplication 类在同一个包中(或在子包中)。

所以你的 Controller 包声明应该是:

package com.hello.springhelloworld.web;

package com.hello.springhelloworld;

Spring Boot 的默认组件扫描会扫描同一个包或主应用程序类包的子包上的组件。

关于java - 为什么我从 hello world SpringBoot 收到 WhiteLabel 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51501166/

相关文章:

java - Jsoup 在指定标签后开始解析还是从页面底部开始?

java - Spring 批处理中的重写 Bean 问题

java - Spring 和 JSR 303 验证。如何仅验证某些属性?

mysql - 为什么在 Spring 应用程序中 mySQL 表上的 Null 列设置为 yes

java - 无法使用 JNDI 连接到 Websphere MQ

java - 无法在 HandlerInterceptorAdapter 中 Autowiring 服务

java - 如何在 freemarker 模板中按索引获取列表项?

java - jackson 的名单保证是可变的吗?

java - 为 Apache Shiro 配置 JTDS 数据源

java - 在 Spring Context @Configuration 中运行一个无效的设置方法