【发布时间】: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