
在使用postman测试spring boot restful api时,常见的404 not found错误往往源于对url路径的误解。本文将深入解析spring boot默认上下文路径的工作机制,结合具体代码示例,指导开发者如何正确构建api请求url,并提供自定义上下文路径的配置方法,旨在帮助用户高效定位并解决此类路径相关的404问题。
当Spring Boot应用程序返回404 Not Found错误时,通常意味着客户端请求的URL与服务器上任何已定义的API端点不匹配。这可能是由于多种原因,但最常见的是URL路径构造不正确,尤其是涉及到应用程序的上下文路径(context path)。
考虑一个Spring Boot应用程序,其目标是管理产品信息并将其存储到MongoDB。开发者尝试使用Postman向http://localhost:8080/mdb-spring-boot-product-organizer/api/addProduct发送POST请求,但收到了以下404错误响应:
{
"timestamp": "2022-12-07T22:56:33.866+00:00",
"status": 404,
"error": "Not Found",
"path": "/mdb-spring-boot-product-organizer/api/addProduct"
}这个错误明确指出,服务器在/mdb-spring-boot-product-organizer/api/addProduct这个路径上没有找到对应的资源。
为了理解为什么会出现404错误,我们需要查看Spring Boot应用程序的控制器代码。
ProductController.java
package com.example.mdbspringbootproductorganizer.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.mdbspringbootproductorganizer.model.Product;
import com.example.mdbspringbootproductorganizer.repository.ProductRepository;
@RestController
@RequestMapping("/api") // 控制器级别的基础路径
public class ProductController {
@Autowired
private ProductRepository repository;
@PostMapping("/addProduct") // 方法级别的路径
public String saveProduct(@RequestBody Product product) {
repository.save(product);
return "Added product with id : " + product.getId();
}
// ... 其他API方法 ...
}从上述代码中,我们可以看到:
因此,saveProduct方法在应用程序中的完整URL路径应该是/api/addProduct。
Spring Boot应用程序在嵌入式Servlet容器(如Tomcat、Jetty)中运行时,默认的上下文路径是/。这意味着应用程序会直接响应根路径下的请求,而不会自动包含项目名称或artifact ID作为URL的一部分。
在给定的错误示例中,mdb-spring-boot-product-organizer是项目名称,但它不应该出现在默认的URL路径中。开发者错误地将项目名称当作了应用程序的上下文路径。
基于上述分析,正确的Postman请求URL应该是:
http://localhost:8080/api/addProduct
这里,http://localhost:8080是服务器地址和端口,/api是控制器级别的基础路径,/addProduct是方法级别的路径。
Postman请求示例:
{
"id": 1,
"name": "Laptop",
"listedPrice": 1200.00,
"purchasePrice": 1000.00,
"condition": "New",
"brand": "Dell",
"shelf": "A",
"bin": 101
}虽然Spring Boot的默认上下文路径是/,但在某些场景下,你可能希望为应用程序设置一个特定的上下文路径,例如/my-app。这可以通过在application.properties或application.yml文件中配置server.servlet.context-path属性来实现。
application.properties示例:
server.port=8080 server.servlet.context-path=/mdb-spring-boot-product-organizer
如果配置了上述属性,那么应用程序的根路径将变为/mdb-spring-boot-product-organizer。此时,要访问saveProduct方法,正确的URL将是:
http://localhost:8080/mdb-spring-boot-product-organizer/api/addProduct
这种情况下,原始请求中的URL反而是正确的。因此,在排查404错误时,检查application.properties或application.yml中是否存在server.servlet.context-path配置是至关重要的一步。
通过理解Spring Boot的URL映射机制和上下文路径配置,开发者可以更有效地避免和解决Postman测试中出现的404 Not Found错误。
以上就是Spring Boot应用Postman 404错误排查与路径配置教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号