【发布时间】:2017-12-09 13:57:35
【问题描述】:
我正在尝试运行 spring boot 应用程序,但收到 404 not found 错误。
项目结构:
src/
+- main/
+- java/
| + com/
| + demo/
| SpringBootDemo.java
| + controller/
| HomeController.java
+- resources/
| application.yml
src/
+- main/
+- webapp/
+- WEB-INF/
+- pages/
| home.jsp
HomeController.java
package com.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String getHome() {
System.out.println("Controller");
return "home";
}
}
application.yml
server:
port: 8080
spring:
mvc:
view:
prefix: /WEB-INF/pages/
suffix: .jsp
build.gradle
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = 'com.demo.SpringBootDemo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
// In this section you declare where to find the dependencies of your project
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter-parent')
compile('javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api')
}
jar {
manifest {
attributes(
'Main-Class': 'com.demo.SpringBootDemo'
)
}
}
SpringBootDemo.java
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages="com.demo")
public class SpringBootDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo.class, args);
}
}
home.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello World
</body>
</html>
当我尝试执行代码时,它在输出中显示为
Controller
但是当我访问http://localhost:8080/ 时它会给出Whitelabel Error Page
【问题讨论】:
-
你读过这个吗? docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…您如何构建和运行您的应用程序?我没有看到你的 gradle 构建应用了任何战争插件,所以你可能忽略了文档中的这个重要警告。`
标签: java spring spring-mvc spring-boot