【发布时间】:2022-06-10 17:22:23
【问题描述】:
我有一个带有 GET 映射的 Spring 应用程序。它还具有 Spring Web 依赖项。尽管如此,每当我尝试调用端点时,我都会收到 404 的响应。我正在 docker 上的 3306 端口上运行一个数据库。
@RestController
public class ApiController {
@Autowired
private JobService jobService;
@GetMapping(value="/hello")
public ResponseEntity<Object> getMapping() {
return ResponseEntity.status(HttpStatus.OK).body("String");
}}
build.gradle 文件是:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//MySQL
runtimeOnly 'mysql:mysql-connector-java'
}
连日志都说:
INFO 29374 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
我不明白为什么在调用 localhost:8080/hello 时返回 404。任何帮助将不胜感激。
编辑: 卷曲回复:
* Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET /hello HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Fri, 10 Jun 2022 09:20:47 GMT
<
* Connection #0 to host localhost left intact
{"timestamp":"2022-06-10T09:20:47.552+00:00","status":404,"error":"Not Found","message":"No message available","path":"/hello"}
application.properties 文件:
server.port=8080
#Spring Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
#Quartz Properties
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.threadPool.threadCount=5
【问题讨论】:
-
你能把
curl -vv "localhost:8080/hello"的结果包含进来吗? -
您的 application.properties 或 yaml 文件中有
server.servlet.context-path=条目? -
编辑问题以显示响应和 application.properties 文件
-
此日志如何查找您
Tomcat started on port(s): 8080 (http) with context path ''? -
嗨@sinha-shaurya 你能截取包裹的屏幕截图吗
标签: java spring spring-boot