我正在尝试运行 Spring Boot 应用程序,但收到 404 未找到错误。
项目结构:
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";
}
}
应用程序.yml
server:
port: 8080
spring:
mvc:
view:
prefix: /WEB-INF/pages/
suffix: .jsp
构建.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错误页面
最佳答案
更新 好的,我会努力做得更好。我并不是故意要伤害你......
我认为您需要从 SpringBootServletInitializer
扩展 SpringBootApplication 类才能在 Spring Boot 应用程序中获取 servlet 功能
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
为了能够解析 View ,您必须定义这些属性:
spring.mvc.view.prefix: /WEB-INF/pages
spring.mvc.view.suffix: .jsp
或者当然是它们的 yaml
变体。实际的jsp页面需要放置在src/main/webapp/WEB-INF/pages
这些是我的 pom 中的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
我希望你能自己翻译这个到 gradle 吗?
我的示例 Controller 如下所示:
@Controller
public class HelloController {
@RequestMapping("/")
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
}
希望这将帮助您让您的应用程序正常运行...
关于java - Spring Boot 404 未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47729310/