我正在尝试在Spring Boot上创建一个基本控制器,但是我得到了:
Whitelabel错误页面
此应用程序没有针对/ error的显式映射,因此您看到了 作为后备。
星期五12月14日21:21:36 ART 2018发生意外错误(type = Not 找到,状态= 404)。
无可用消息
错误。我将控制器放在应用程序应该位于的包的子包中。
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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</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>
HomeController.java:
package com.example.demo.home;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home(){
return "home";
}
}
DemoApplication.java:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
控制台输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
2018-12-14 21:16:46.179 INFO 5420 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-BMTCH27 with PID 5420 (C:\Users\Usuario\eclipse-workspace\Project\target\classes started by Usuario in C:\Users\Usuario\eclipse-workspace\Project)
2018-12-14 21:16:46.179 INFO 5420 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-12-14 21:16:51.395 INFO 5420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-14 21:16:51.433 INFO 5420 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-14 21:16:51.433 INFO 5420 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-14 21:16:51.464 INFO 5420 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_192\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_192/bin/server;C:/Program Files/Java/jre1.8.0_192/bin;C:/Program Files/Java/jre1.8.0_192/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;c:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps;;C:\Users\Usuario\Desktop;;.]
2018-12-14 21:16:51.871 INFO 5420 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2018-12-14 21:16:51.871 INFO 5420 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-14 21:16:51.871 INFO 5420 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5489 ms
2018-12-14 21:16:52.449 INFO 5420 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-14 21:16:52.959 INFO 5420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-14 21:16:52.975 INFO 5420 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 8.155 seconds (JVM running for 9.247)
2018-12-14 21:17:00.448 INFO 5420 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-14 21:17:00.448 INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-12-14 21:17:00.462 INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
答案 0 :(得分:1)
它可以来自@Controller注释。请更改为@RestController
摘自Spring Boot文档https://spring.io/guides/gs/rest-service/
在Spring建立RESTful网络服务的方法中,HTTP请求由控制器处理。这些组件可以通过@RestController批注轻松识别,并且下面的GreetingController通过返回Greeting类的新实例来处理GET请求/ greeting:
答案 1 :(得分:0)
@Controller注释表示Spring MVC控制器。如果用@Controller批注标记类,则必须在处理程序方法上放置@ResponseBody。
另一方面,@ RestController注释是@Controller的特殊版本,它会自动添加@Controller和@ResponseBody注释,因此无需将@ResponseBody添加到我们的处理程序方法中。
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "home";
}
}
或
@Controller
public class HomeController {
@RequestMapping("/")
@ResponseBody
public String home(){
return "home";
}
}
答案 2 :(得分:0)
有关信息:如果您未指定任何映射,则此方法将解析所有http请求,即,您可以将GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
请求发送到指定的url,它将被解析。
对于您而言,只需将控制器更改为:
package com.example.demo.home;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HomeController {
@RequestMapping("/") // Or you can do : @GetMapping("/")
public String home(){
return "home";
}
}
网址:
http://localhost:8080/
答案 3 :(得分:0)
对于您的代码/配置,它将在html
或static
文件夹下查找webapp
文件,同时您的主控制器应返回home.html
而不是{{1} }。
答案 4 :(得分:0)
Thymeleaf应该作为依赖项包含在项目的pom.xml中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
也应将home.html文件从模板移动到静态目录。
有关更多详细信息,请查看Serving Web Content with Spring MVC。