Parth Jeet
Parth Jeet

Reputation: 129

In Spring Data Rest, How to prevent DELETE HTTP methods from being exported from my JpaRepository?

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

@RepositoryRestResource(path = "projects")
public interface ProjectsRepository extends JpaRepository<MetricsProjects, Integer> {...}

My repository interface:

@RepositoryRestResource(path = "projects")
public interface ProjectsRepository extends JpaRepository<MetricsProjects, Integer> {
    
    List<MetricsProjects> findByProjectName(String projectName);
    
    @Override
    @RestResource(exported = false)
    public void deleteById(Integer id);
    
    @Override
    @RestResource(exported = false)
    public void delete(MetricsProjects entity);
    
    @Override
    @RestResource(exported = false)
    public void deleteAll(Iterable<? extends MetricsProjects> entities);
    
    @Override
    @RestResource(exported = false)
    public void deleteAll();
    
    @Override
    @RestResource(exported = false)
    public void deleteInBatch(Iterable<MetricsProjects> entities);
    
    @Override
    @RestResource(exported = false)
    public void deleteAllInBatch();

}

I've also added disableDefaultExposure() , as suggested somewhere.

My Configuration file:

@Configuration
public class SpringDataRestConfiguration implements RepositoryRestConfigurer {
    
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
        restConfig.disableDefaultExposure();
    }

}

But I still see the DELETE methods exposed from my Swagger-UI, how do I prevent this?

Upvotes: 3

Views: 868

Answers (3)

abdella
abdella

Reputation: 754

Modify the configureRepositoryRestConfiguration to

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration restConfig) {
        restConfig.getExposureConfiguration()
                .forDomainType(put the class type here)
                .withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.DELETE))
                .withCollectionExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.DELETE));
    }

Replace put the class type here with the className.class, for example if the className is MetricsProjects, put MetricsProjects.class there.

Here is additional info.

Upvotes: 1

Punyan
Punyan

Reputation: 1

To disable the display of a specific endpoint in Swagger (Springfox library), I have added the annotation ApiIgnore on the associated method in repository class :

 @Override
 @ApiIgnore
 @RestResource(exported = false)
 public void deleteById(Integer id);

Upvotes: 0

Selindek
Selindek

Reputation: 3423

Create a controller method for the DELETE endpoint and return 405 Method Not Allowed.

Upvotes: 0

Related Questions