spring boot之Whitelabel Error Page

最近在系统性学习spring boot,边学边动手写写小程序,话说第一个程序还真碰到不少问题。

第一步:创建一个SpringBoot 工程,工程结构如下:

第二步:写第一个spring boot程序,按照程序员的命令习惯,第一个程序都会叫XXX helloworld,这里也不例外,第一个spring boot程序就叫HelloWorldController,代码也比较简单

将工程跑起来之后,在浏览器访问http://localhost:8080/helloWorld/index

得到的结果

Whitelabel Error Page

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

Wed Aug 21 22:16:56 CST 2019

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

No message available

看错误信息,貌似是应用访问映射错误,查看日志,没有任何报错,代码能够正常运行,难道理解错了?

度娘上搜索一把, 发现有不少网友都遇到这个问题,网友说导致这个报错的原因有好几个,刚才有不少朋友也分享了他们遇到的情况及处

理方法,都试了一下终于找到解决办法:要将Application(入口类)放在最外层,SpringBoot因为默认不指定basePackages,如果不是默

认路径,可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围。

根据我的项目结构只需要将DemoApplication入口类的package改为com.xbrother即可

修改之后,终于看到了熟悉的hello world!
 ———————————————— 

 

 

 

 

使用http://localhost:8887/hello?name=community **返回 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback Sat Nov 23 17:42:18 CST 2019 There was an unexpected error (type=Not Found, status=404). No message available **编译器run 2019-11-23 17:38:20.780 INFO 9072 --- [ main] c.x.community.CommunityApplication : Starting CommunityApplication on SW-PC with PID 9072 (G:\community\target\classes started by SW in G:\community) 2019-11-23 17:38:20.784 INFO 9072 --- [ main] c.x.community.CommunityApplication : No active profile set, falling back to default profiles: default 2019-11-23 17:38:22.429 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8887 (http) 2019-11-23 17:38:22.440 INFO 9072 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-11-23 17:38:22.440 INFO 9072 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27] 2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1678 ms 2019-11-23 17:38:22.750 INFO 9072 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-11-23 17:38:23.136 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8887 (http) with context path '' 2019-11-23 17:38:23.140 INFO 9072 --- [ main] c.x.community.CommunityApplication : Started CommunityApplication in 2.996 seconds (JVM running for 4.612) 比老师多了这几行 2019-11-23 17:38:35.007 INFO 9072 --- [nio-8887-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-11-23 17:38:35.008 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-11-23 17:38:35.016 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms** ![图片说明](https://img-ask.csdn.net/upload/201911/23/1574520307_292684.png) **代码** G:\community\src\main\java\com\xjuygur\community\controller\HelloController.java ``` package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController{ @GetMapping("/hello") public String hello(@RequestParam(name="name") String name, Model model){ model.addAttribute("name",name); return "hello"; } } ``` **html G:\community\src\main\resources\templates\hello.html ``` <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html> ``` **pom.xml** G:\community\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.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xj-uygur</groupId> <artifactId>community</artifactId> <version>0.0.1-SNAPSHOT</version> <name>community</name> <description>community</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-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ```
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页