我创建了一个 spring-boot 项目,并为
/employers
定义了一个带有 1 个端点的 REST 控制器
@RestController
public class EmployerController {
@Autowired
private EmployerService employerService;
@GetMapping(path = "/employers")
public List<Employer> getEmployers() {
return (List<Employer>) employerService.getEmployers();
}
}
此外,我还创建了关联的 EmployerService、EmployerRepository 和 Employer 实体
当我将 Swagger/OpenAPI 添加到类路径,然后导航到
http://localhost:8080/swagger-ui.html
时,雇主实体有一个完整的 CRUD REST 定义,其中包括 GET/POST/PUT/DELETE/PATCH
当我只为
GET /employers
定义单个端点时,为什么要为雇主定义完整的 CRUD REST API?如果这是可配置的,是否可以禁用 REST API 的自动创建?
我的猜测是,您正在使用
spring-boot-starter-data-rest
,我相信一旦您创建了 @Entity
带注释的类,它就会自动公开这些 CRUD 端点。
那么,你的 build.gradle 或 pom.xml 中的 spring 依赖项是什么?我想您使用的不仅仅是通常的
spring-boot-starter
和 spring-boot-starter-web
依赖项。