How to set default value of exported as false in rest resource spring data rest

前端 未结 4 618

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

4条回答
  •  终归单人心
    2021-01-20 09:00

    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
    
    }
    

提交回复
热议问题