找不到Spring Boot 404错误

时间:2017-12-09 13:57:35

标签: java spring spring-mvc spring-boot

我正在尝试运行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";
    }
}

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

2 个答案:

答案 0 :(得分:2)

<强>更新 好的,我会尝试做得更好。我不打算做错你......

我认为你需要从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

这些是我朋友的依赖

   <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>

我希望你能自己翻译一下吗?

我的样本控制器如下所示:

@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";
    }
}

希望这可以帮助您使应用程序正常运行......

答案 1 :(得分:1)

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

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

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