Spring Boot报错:Whitelabel Error Page

  • A+
所属分类:Java

 

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 09 21:58:45 CST 2019

There was an unexpected error (type=Not Found, status=404).

No message available

Spring Boot报错:Whitelabel Error Page

 

 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 http://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.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>demo.springboot</groupId>
    <artifactId>chapter-1-springboot-quickstart</artifactId>
    <version>0.0.1</version>
    <name>chapter-1-springboot-quickstart</name>
    <description>《Spring Boot 2.x 学习》</description>

    <properties>
        <java.version>1.8</java.version>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>
//Chapter1SpringbootQuickstartApplication.java

package demo.springboot;
/* *
 * @Author: openshell
 * @Description:Spring Boot 应用启动类
 */



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages ={"demo.spring.web"})
public class Chapter1SpringbootQuickstartApplication {

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

}
//HelloController.java
package demo.spring.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @RequestMapping(value="/hello", method= RequestMethod.GET)
    @ResponseBody
    public String sayHello(){
        return "Hello Spring Boot!";
     }
}

最近学习Spring Boot,开始照着网上的教程搭建个小demo,估计是网上教程有误,因为自己新建的Controller与主类没有在同一个包。程序默认只加载application.java 所在包及其子包下的内容。

 

解决方案一:

加入注解:@ComponentScan(basePackages ={"demo.spring.web"})

解决方案二:

更改包的目录结构,将类跟Application.java放置同一个包中或者放置在Application.java的子包中。


发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: