How to prevent some HTTP methods from being exported from my MongoRepository?

后端 未结 3 2075
孤街浪徒
孤街浪徒 2020-12-08 01:07

I\'m using spring-data-rest and I have a MongoRepository like this:

@RepositoryRestResource
interface MyEntityRepository extends MongoRepository

        
相关标签:
3条回答
  • 2020-12-08 01:26

    This is late reply, but if you need to prevent the global http method for a entity, try it.

    @Configuration
    public class DataRestConfig implements RepositoryRestConfigurer {
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
             config.getExposureConfiguration()
                    .forDomainType(Person.class)
                    .withItemExposure(((metdata, httpMethods) -> httpMethods.disable(HttpMethod.PUT, HttpMethod.POST, ... )))
                    .withCollectionExposure((metdata, httpMethods) -> httpMethods.disable(HttpMethod.PUT, HttpMethod.POST, ...));
        }
    }
    
    0 讨论(0)
  • Why not just use like this?

    @Configuration
    public class SpringDataRestConfiguration implements RepositoryRestConfigurer {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
            restConfig.disableDefaultExposure();
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:42

    Thanks to Oliver, here are the methods to override:

    @RepositoryRestResource(collectionResourceRel = "people", path = "people")
    public interface PersonRepository extends MongoRepository<Person, String> {
    
        // Prevents GET /people/:id
        @Override
        @RestResource(exported = false)
        public Person findOne(String id);
    
        // Prevents GET /people
        @Override
        @RestResource(exported = false)
        public Page<Person> findAll(Pageable pageable);
    
        // Prevents POST /people and PATCH /people/:id
        @Override
        @RestResource(exported = false)
        public Person save(Person s);
    
        // Prevents DELETE /people/:id
        @Override
        @RestResource(exported = false)
        public void delete(Person t);
    
    }
    
    0 讨论(0)
提交回复
热议问题