Search code examples
spring-bootswaggeropenapispring-rest

Spring Boot: Full CRUD REST endpoints are auto-generated and shown in SwaggerUI


I’ve created a spring-boot project and defined a REST controller with 1 endpoint for /employers

@RestController
public class EmployerController {

    @Autowired
    private EmployerService employerService;

    @GetMapping(path = "/employers")
    public List<Employer> getEmployers() {
        return (List<Employer>) employerService.getEmployers();
    }
}

Additionally, I’ve created the associated EmployerService, EmployerRepository, and Employer entity

When I add Swagger/OpenAPI to the classpath, and then navigate to http://localhost:8080/swagger-ui.html, there is a full CRUD REST definition for the Employer entity which includes GET/POST/PUT/DELETE/PATCH

Full CRUD Rest Definition

Why is there a full CRUD REST API defined for Employer when I’ve only defined a single endpoint for GET /employers? If this is configurable, is it possible to disable the auto-creation of the REST API?


Solution

  • My guess is that you're using spring-boot-starter-data-rest which I believe would auto expose those crud endpoints once you create an @Entity annotated class, and a CrudRepository.

    So, what are your spring dependencies in your build.gradle or pom.xml? I would imagine you're using something other than just the usual spring-boot-starter and spring-boot-starter-web dependencies.

    In any case, I don't think all of those those endpoints are coming from the one @GetMapping in the snippet you provided. We would have to see the dependencies and the rest of your setup to say for sure. Specifically, what is the EmployerRepository implementation extending? If it's a CrudRepository and you're using spring-boot-starter-data-rest I believe that would explain it.

    Here are the docs https://docs.spring.io/spring-data/rest/reference/repository-resources.html