I want to use RestResource annotation of spring data rest. As you know it exposes ALL CRUD methods by default. But I only need findAll method. One way is to set exported val
To extend on the answer of Imran Tahir which helped me - thanks Imran! - you can override this for your entire repository by setting ExposeRepositoryMethodsByDefault to false.
@Component
public class SpringRestConfiguration extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setExposeRepositoryMethodsByDefault(false);
}
}
The detection strategy can now be set in from your configuration file. (In my case YAML)
spring:
data:
rest:
base-path: /api/base/path
detection-strategy: annotated
And then in your repository only add @RestResource to (overridden) methods you need to have enabled:
@RepositoryRestResource(collectionResourceRel = "someThing", path = "someThing")
@PreAuthorize("hasRole('ROLE_ADMIN') OR hasRole('ROLE_USER')")
public interface SomeThingRepository extends PagingAndSortingRepository {
@Override
@RestResource
Page findAll(Pageable pageable);
@Override
@RestResource
Optional findById(Long aLong);
// All other methods are not exposed
}