【问题标题】:Spring Boot 404 not found errorSpring Boot 404 未找到错误
【发布时间】: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

【问题讨论】:

标签: java spring spring-mvc spring-boot


【解决方案1】:

更新 好的,我会努力做得更好。我不是故意要害你的……

我认为您需要从 SpringBootServletInitializer 扩展 SpringBootApplication 类以在您的 Spring Boot 应用程序中获取 servlet 功能

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

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

为了能够解析视图,您必须定义这些属性:

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
public class HelloController {
    @RequestMapping("/")
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}

希望这将帮助您让您的应用正常运行...

【讨论】:

  • 这个链接给出了关于 REST Web 服务的教程,但是我卡在了 Web 应用的问题上。这个链接没有给出答案,因为我已经搜索了教程但仍然停留在问题中。请让我知道我哪里出错了。
  • 对不起,我无意给出一个错误的答案。希望这个会做得更好...
  • 在 build.gradle 文件中添加 compile("org.springframework.boot:spring-boot-starter-thymeleaf") 并将 home.html 放入 resource/templates 文件夹后,我就可以正常工作了。
  • 也可能有助于将 tomcat-embed-jasper 依赖项的范围从 provided 更改为 compile。为我工作!
【解决方案2】:

使用 maven spring boot 目标运行:spring-boot:run

在 IntelliJ 中设置 maven 配置的步骤:

调试/运行配置 |单击左上角可见的 + 按钮 |选择 Maven |将命令行设置为spring-boot:run

【讨论】: