【问题标题】:Spring boot always returns a 404 errorSpring Boot 总是返回 404 错误
【发布时间】:2018-07-15 07:02:33
【问题描述】:

我正在尝试让 Spring Boot (1.5.9) 项目以简单的 Hello world 消息进行响应。但是,我发送的任何请求都会立即返回 404 异常。

MyBackendApplication

package net.mypackage.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyBackendApplication {

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

控制器/HelloController

package net.mypackage.backend.controllers;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "Hello, world!";
    }

}

application.properties

server.port = 5000
server.contextPath=/
logging.level.org=DEBUG

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'net.mypackage'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-jersey')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("org.springframework.boot:spring-boot-starter-actuator")
}

请求页面 (localhost:5000/hello) 时,我收到 404 错误。以下信息显示在此请求的日志中(here 是一个要点,因为我不喜欢将其格式化为引号的方式):

2018-02-05 13:01:37.600 调试 17448 --- [nio-5000-exec-2] o.a.coyote.http11.Http11InputBuffer : 收到 [GET / HTTP/1.1
缓存控制:无缓存用户代理:PostmanRuntime/7.1.1 接受: / 主机:127.0.0.1:5000 接受编码:gzip,放气连接:保持活动] 2018-02-05 13:01:37.601 调试 17448 --- [nio-5000-exec-2] o.a.c.authenticator.AuthenticatorBase:安全检查请求 GET / 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] org.apache.catalina.realm.RealmBase:没有定义适用的约束 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] o.a.c.authenticator.AuthenticatorBase:不受任何约束 2018-02-05 13:01:37.604 DEBUG 17448 --- [nio-5000-exec-2] o.a.tomcat.util.net.SocketWrapperBase:套接字: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel[已连接 本地=/127.0.0.1:5000 远程=/127.0.0.1:53684]],从缓冲区读取:[0] 2018-02-05 13:01:37.604 调试 17448 --- [nio-5000-exec-2] o.apache.coyote.http11.Http11Processor:套接字: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel[已连接 本地=/127.0.0.1:5000 远程=/127.0.0.1:53684]],状态: [OPEN_READ],说明:[OPEN]

我确定我错过了一些非常基本的东西,但我终其一生都无法弄清楚是什么。在此先感谢:)

【问题讨论】:

  • 首先,如果您不想保留上下文路径,请从那里删除server.contextPath。默认情况下它会在ip:port
  • @user3145373ツ谢谢,我在别处看到建议添加/ 的上下文路径。我从我的应用程序配置中删除了它,但仍然有同样的问题。
  • 能否列出 pom.xml 配置和依赖项。
  • 从日志看来它正在寻找 / 而不是 /hello - Security checking request GET /
  • 修复你的 gradle 依赖。它应该有compile("org.springframework.boot:spring-boot-starter-web"),实际上只是尝试:dependencies {compile("org.springframework.boot:spring-boot-starter-web")}

标签: java spring-boot


【解决方案1】:

您提供的信息不够。

当我用这个 pom.xml 尝试你的应用程序和控制器时:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mvc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MVC</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

它适用于server.contextPath=/,也适用于注释掉。

【讨论】:

    【解决方案2】:

    此代码适用于我,除了以下代码之外没有单个字符可以运行 Spring Boot 简单 API。我认为您的应用程序中存在一些配置类或 pom 问题。

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.10.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    HelloController.java

    package com.example.demo;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String sayHello() {
            return "Hello, world!";
        }
    }
    

    DemoApplication.java

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    application.properties

    server.port = 5000
    logging.level.org=DEBUG
    

    运行成功

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2020-02-03
      • 1970-01-01
      • 2012-04-02
      • 2012-08-16
      • 1970-01-01
      相关资源
      最近更新 更多