SpringBoot:Whitelabel Error Page解决方案

早就听闻SpringBoot的大名,最近自己就尝试搞了个SpringBoot项目。随便弄了个测试类:

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>

	<groupId>com.yinhe.test</groupId>
	<artifactId>SpringBootTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootTest</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<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>

 SpringBootTestApplication.java:

package com.yinhe.test.SpringBootTest;

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

@SpringBootApplication
public class SpringBootTestApplication {

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

TestController.java:

package com.yinhe.test.controller;

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

@RestController
@RequestMapping("/myTest")
public class TestController {
	@RequestMapping("/hello")
	public String hello(){
		
		return "HelloWorld!!!";
	}
}

 接着运行 SpringBootTestApplication类中的main方法,访问http://localhost:8080/myTest/hello出现:

SpringBoot:Whitelabel Error Page解决方案

原因是:程序只加载Application.java(SpringBootTestApplication.java)所在包及其子包下的内容

 解决方法有两种:

一、加入注解:@ComponentScan(basePackages = {"com.yinhe.test.controller"})

SpringBoot:Whitelabel Error Page解决方案

 二、更改包的目录结构

将类跟Application.java放置同一个包中或者放置在Application.java的子包中

 

这样问题就解决了! 0_0