为啥 Postman 找不到我的 Spring REST API?
Posted
技术标签:
【中文标题】为啥 Postman 找不到我的 Spring REST API?【英文标题】:Why can't Postman find my Spring REST API?为什么 Postman 找不到我的 Spring REST API? 【发布时间】:2021-12-12 06:52:22 【问题描述】:我使用 REST API 创建了一个非常基本的 Spring Boot 项目。我尝试将它连接到我的 Angular 应用程序,但它遇到了一些 CORS 安全错误,所以我切换到 Postman。我正在尝试使用 Postman 对其进行测试,但我一直在 Postman 上收到 404 not found 错误。为什么我无法连接到我的后端并发布到测试仪功能?
控制器
package controllers;
@RestController
public class Handler
@PostMapping("/tester/id")
public String tester(@PathVariable(value="id")long userid )
System.out.println("in tester");
System.out.println(userid);
return "succeeded test";
主要
package halloween.contest;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class)
public class ContestApplication
public static void main(String[] args)
SpringApplication.run(ContestApplication.class, args);
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.data.rest.base-path=/
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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>halloween</groupId>
<artifactId>contest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contest</name>
<description>halloween picture contest</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
控制台
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-26 15:44:12.823 INFO 18760 --- [ main] halloween.contest.ContestApplication : Starting ContestApplication using Java 1.8.0_144 on DESKTOP-VB80TS0 with PID 18760 (C:\Users\Sergio Rodríguez\Documents\Halloween\contest\target\classes started by Sergio Rodríguez in C:\Users\Sergio Rodríguez\Documents\Halloween\contest)
2021-10-26 15:44:12.827 INFO 18760 --- [ main] halloween.contest.ContestApplication : No active profile set, falling back to default profiles: default
2021-10-26 15:44:16.013 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-26 15:44:16.043 INFO 18760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-26 15:44:16.043 INFO 18760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-26 15:44:16.310 INFO 18760 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-26 15:44:16.310 INFO 18760 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3372 ms
2021-10-26 15:44:19.326 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-26 15:44:19.342 INFO 18760 --- [ main] halloween.contest.ContestApplication : Started ContestApplication in 7.201 seconds (JVM running for 7.946)
2021-10-26 15:45:45.138 INFO 18760 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-26 15:45:45.139 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-26 15:45:45.157 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
等效的 cURL 命令
curl --location --request POST 'localhost:8080/tester/69'
邮递员
【问题讨论】:
您在 Postman 中使用的是哪个 HTTP 动词和 URL? 你能发布 Postman 请求吗? @JoãoDias 嗨对不起,我忘了添加图片。我已经进行了编辑 谢谢,如果您转到邮递员右侧的“代码”选项卡,您可以看到请求的整个 curl 命令,您也可以发布。 @SandeepKamath 添加 【参考方案1】:既然我想通了,就回答我自己的问题。根据我提供的代码,这并不明显,但我会留下我的帖子以供将来帮助(措辞帮助)。
问题在于包的可见性。我的控制器类与主类位于不同的包中。
【讨论】:
接受您的正确答案;) 公平地说,您确实列出了主要和控制器类的包名称,因此您的问题中存在信息。【参考方案2】:要解决 CORS 问题,请在 @PostMapping("/tester/id")
旁边添加 @CrossOrigin(origins = "http://localhost:<port>")
,其中 port 是运行 Angular 应用程序的端口。
在您的目标 URL 中,确保您已包含默认的 Spring Boot 端口 8080,例如http://localhost:8080/tester/123
很明显,请仔细检查您使用的是 POST
而不是 GET
。
【讨论】:
以上是关于为啥 Postman 找不到我的 Spring REST API?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在使用 Spring Boot Application 的邮递员中没有收到错误消息
spring-cloud导入eclipse时,slf4j注解为啥找不到log变量
为啥运行“pip install -r requirements.txt”时出现错误“找不到满足要求 scipy==1.5.3 的版本”?