Search code examples
javaspringspring-bootspring-dataspring-restcontroller

Why does RestController expose all the operations in CrudRepository?


Setup

I have a PagingAndSortingRepository from which I want to expose only a limited set of (mostly) read operations and add some of non-DB services. I added a REST controller to front the crud repository.

Problem

On Swagger interface, I see all the operations enabled, even if only one operation is called from the rest repo. All the operations get the same path ,e.g. "/rest/foo" in below example.

How can I disable Spring Boot injecting all the operations?

Additional Observations :

  • If I do not have any method using crud repo in the rest controller, operations are not listed. Even though if the crud repo is auto wired.
  • I did not want to disable each operation in CRUD Repo individually. Even if I do that Swagger would list the operations, but calls would fail with 405.

Sample Code

public interface MyCRUDRepository extends PagingAndSortingRepository<Foo, FooPK> {
}

@RestController
public class MyRESTController {

  @Autowired
  MyCRUDRepository repository;

  @RequestMapping("/rest/foo")
  public Foo find(String id) {
    return repository.findOne(id);
  }
}

Solution

  • Two changes I made to fix the issue :

    1. Added a method attribute (GET) in RequestMapping. Without this all the methods were allowed, bound to the same method.
    2. As suggested by @mrkernelpanic, adding @ApiOperation to the method rectified the error where Swagger added all the methods to it's reported API.