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

感情迁移 提交于 2019-12-18 10:34:39

问题


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

@RepositoryRestResource
interface MyEntityRepository extends MongoRepository<MyEntity, String> {
}

I would like to allow the GET methods but disable PUT, POST, PATCH and DELETE (read only web service).

According to http://docs.spring.io/spring-data/rest/docs/2.2.2.RELEASE/reference/html/#repository-resources.collection-resource I should be able to do that like this:

@RepositoryRestResource
interface MyEntityRepository extends MongoRepository<MyEntity, String> {

    @Override
    @RestResource(exported = false)
    public MyEntity save(MyEntity s);

    @Override
    @RestResource(exported = false)
    public void delete(String id);

    @Override
    @RestResource(exported = false)
    public void delete(MyEntity t);
}

It doesn't seem to work as I can still do PUT, POST, PATCH and DELETE requests.


回答1:


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);

}



回答2:


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, ...));
    }
}


来源:https://stackoverflow.com/questions/29169717/how-to-prevent-some-http-methods-from-being-exported-from-my-mongorepository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!