springboot 项目,在哪儿看接口路径名称的配置?
·
在 SpringBoot 项目中,接口路径的配置可以在多个地方查看。以下是主要的配置位置:
1. 控制器类注解配置
类级别的 @RequestMapping
@RestController
@RequestMapping("/api/users") // 基础路径
public class UserController {
@GetMapping("/list") // 完整路径: /api/users/list
public List<User> listUsers() {
// ...
}
@PostMapping("/create") // 完整路径: /api/users/create
public User createUser() {
// ...
}
}
2. 方法级别的映射注解
@RestController
public class ProductController {
@GetMapping("/products") // 路径: /products
public List<Product> getProducts() {
// ...
}
@PostMapping("/product") // 路径: /product
public Product createProduct() {
// ...
}
@PutMapping("/product/{id}") // 路径: /product/{id}
public Product updateProduct() {
// ...
}
}
3. 全局路径前缀配置
application.properties
server.servlet.context-path=/myapp
# 所有接口都会加上 /myapp 前缀
application.yml
server:
servlet:
context-path: /myapp
4. 查看所有注册的接口
方式一:通过 Actuator 端点
# 在 application.properties 中启用
management.endpoints.web.exposure.include=*
management.endpoint.mappings.enabled=true
访问:http://localhost:8080/actuator/mappings
方式二:启动日志中查看
SpringBoot 启动时会在日志中输出所有映射的接口:
Mapped "{[/api/users],methods=[GET]}" ...
Mapped "{[/api/users/{id}],methods=[GET]}" ...
方式三:使用 Spring Boot DevTools
在开发环境中,Spring Boot DevTools 会在应用启动时显示所有映射的端点。
5. 自定义 Servlet 路径
# 为所有控制器设置统一前缀
spring.mvc.servlet.path=/v1
6. 完整的路径计算规则
完整接口路径 = server.servlet.context-path + spring.mvc.servlet.path + @RequestMapping路径
实际示例
// application.yml
server:
servlet:
context-path: /myapp
spring:
mvc:
servlet:
path: /v1
// Controller
@RestController
@RequestMapping("/api")
public class TestController {
@GetMapping("/hello") // 完整路径: /myapp/v1/api/hello
public String hello() {
return "Hello World";
}
}
建议:在开发过程中,最方便的方式是查看启动日志或访问 /actuator/mappings 端点来确认所有接口的实际路径。
更多推荐


所有评论(0)