java Spring Boot 404 未找到错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47729310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:46:50  来源:igfitidea点击:

Spring Boot 404 not found error

javaspringspring-mvcspring-boot

提问by Harshit Shrivastava

I am trying to run the spring boot application but getting the 404 not found error.

我正在尝试运行 Spring Boot 应用程序,但出现 404 not found 错误。

Project structure:

项目结构:

src/
 +- main/
     +- java/
     |   + com/
         |   + demo/
             |    SpringBootDemo.java
             |    + controller/
                  |    HomeController.java
     +- resources/
             |   application.yml


src/
 +- main/
     +- webapp/
        +- WEB-INF/
           +- pages/
              | home.jsp

HomeController.java

主控制器.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

应用程序.yml

server:
  port: 8080
spring:
  mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .jsp

build.gradle

构建.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

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

主页.jsp

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    Hello World
</body>
</html>

When I try to execute the code, it shows in the output as

当我尝试执行代码时,它在输出中显示为

Controller

But it gives the Whitelabel Error Pagewhen I visit http://localhost:8080/

但是Whitelabel Error Page当我访问http://localhost:8080/时它给出了

回答by Ivonet

UPDATEOk I'll try to do better. It was not my intention to do you wrong...

更新好的,我会努力做得更好。我不是有意要冤枉你...

I think you need to extend the SpringBootApplication class from SpringBootServletInitializerto get servlet functionality in your Spring Boot application

我认为您需要扩展 SpringBootApplication 类 SpringBootServletInitializer才能在 Spring Boot 应用程序中获得 servlet 功能

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

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

In order to be able to resolve the view you have to define these properties:

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

spring.mvc.view.prefix: /WEB-INF/pages
spring.mvc.view.suffix: .jsp

or of course the yamlvariant of them. The actual jsp pages need then be placed in src/main/webapp/WEB-INF/pages

或者当然yaml是它们的变体。实际的jsp页面需要放在src/main/webapp/WEB-INF/pages

These are the dependencies in my pom

这些是我的 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>

I hope you can translate this to gradle yourself?

我希望你能把这个翻译成自己的 gradle 吗?

My sample controller looks like this:

我的示例控制器如下所示:

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

Hope this will help you with getting your app working...

希望这可以帮助您让您的应用程序正常工作...

回答by Techflash

Run with maven spring boot goal: spring-boot:run

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

Steps to setup maven configuration in IntelliJ:

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

Debug/Run Configuration | Click on + button visible at top left | Select Maven | Set command line to spring-boot:run

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