Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

Spring Data Rest - Base Repository using Generics not working

I want to disable export of DELETE operation using Spring Data rest using generics. Any repository which extends from BaseResourceRepository should not export DELETE verb. I am using groovy

@NoRepositoryBean
interface BaseResourceRepository<T extends BaseEntity, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    @Override
    @RestResource(exported = false)
    void delete(T t)
}

@RepositoryRestResource(collectionResourceRel = "contacts", path = "contacts")
interface ContactRepository extends BaseResourceRepository<Contact, Long> {

}

I want to disable the DELETE operation for /contacts endpoint

This configuration is still allowing me to DELETE the contact resource

Any help is appreciated.

Thanks in advance

question from:https://stackoverflow.com/questions/65883173/spring-data-rest-base-repository-using-generics-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See the official documentation regarding hiding CRUD methods: https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.hiding-repository-crud-methods

As mentioned there you need to hide both methods. Should look like this (may contain errors due to no IDE present ;))

@NoRepositoryBean
interface BaseResourceRepository<T extends BaseEntity, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    @Override
    @RestResource(exported = false)
    void delete(T t)

    @Override
    @RestResource(exported = false)
    void delete(ID id);
}

Edit

An approach to give a try to

The base repository

package com.stackoverflow.generics.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;

@NoRepositoryBean
public interface NoDeleteRepository <T, ID> extends Repository<T, ID> {
    T findOne(ID id);
    Iterable<T> findAll();
    Iterable<T> findAll(Sort sort);
    Page<T> findAll(Pageable pageable);
   // Define other necessary methods
}

The repository for an concrete entity

package com.stackoverflow.generics.repository;

import com.stackoverflow.generics.repository.SomeEntity;

public interface SomeNoDeleteRepository extends NoDeleteRepository<SomeEntity, Long> {
}

The repository does not expose the delete method therefore I assume it wont be exposed as REST endpoint. Can't test it as my spring does not expose all repositories as rest resource. Some misconfiguration I think.

public class SomeService {
        private final SomeNoDeleteRepository repository;

    public void deleteSome(Long id) {
        // cannot resolve method delete
        repository.delete(id);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...