【发布时间】:2020-10-07 02:12:18
【问题描述】:
我使用 Spring Boot 已经有一段时间了,但想创建一个新的应用程序,从一个简单的 REST 服务开始。无论我尝试什么,我的服务都会不断返回 404 错误,我无法找出原因。我在网上发现了一些类似的问题,但它们几乎总是因为 Controller 类不在正确的包中,但这里不是这种情况。为了排除端口问题,我将其从 8080->8901(和其他几个)更改但无济于事。我还尝试了 @ComponentScan ,但它不起作用。以下是文件:
应用类:
package nl.haba.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
控制器类:
package nl.haba.demo.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class GreetingService {
@GetMapping("/greet")
public ResponseEntity<String> greetPerson() {
return ResponseEntity.ok().body("Hello, World");
}
}
Application.yml
server:
port: 8901
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>nl.haba</groupId>
<artifactId>mydemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mydemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
春季启动日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-06-17 11:15:06.650 INFO 11251 --- [ main] n.h.demo.DemoApplication : Starting DemoApplication on XXXXXXXXXXXXX with PID 11251 (/Users/xxxxxxxxxx/Projects/mydemo/target/classes started by xxxxxxxxxx in /Users/xxxxxxxxxx/Projects/mydemo)
2020-06-17 11:15:06.653 INFO 11251 --- [ main] n.h.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-06-17 11:15:07.357 INFO 11251 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8901 (http)
2020-06-17 11:15:07.365 INFO 11251 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-17 11:15:07.366 INFO 11251 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-06-17 11:15:07.442 INFO 11251 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-17 11:15:07.442 INFO 11251 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 748 ms
2020-06-17 11:15:07.586 INFO 11251 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8901 (http) with context path ''
2020-06-17 11:15:07.598 INFO 11251 --- [ main] n.h.demo.DemoApplication : Started DemoApplication in 1.282 seconds (JVM running for 1.921)
当我使用 Postman 获取结果时
GET http://localhost:8901/greet
它总是返回 404,我不知道为什么。卷曲也不起作用。 有人可以帮助我并告诉我我在这里缺少什么吗?
【问题讨论】:
-
可能缺少依赖 spring-boot-starter-web?
-
可能确实如此。 @Haba 这个链接可能有用:spring.io/guides/gs/rest-service
-
是的,你添加了
spring-boot-starter-jersey依赖,它需要不同的注解来定义控制器。看看这个例子:zetcode.com/springboot/jersey 另一种选择是将spring-boot-starter-jersey替换为spring-boot-starter-web。这样您就不需要更改控制器。
标签: java spring-boot rest