Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest Custom Controller

I have requirement where in which i need to override the delete functionality from the rest Resource using a custom controller.here is the code for restResource

@RepositoryRestResource
    public interface SampleRepository extends JpaRepository<Sample,Long>{
List<Sample> findBySampleNumber(@Param("sampleNumber") String sampleNumber);
    }

i have created a a custom controller which overides only delete fuctionality

@RepositoryRestController
@RequestMapping("/api/samples")
public class SampleController{
    @Autowired
    SampleRepository sampleRepository;

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable Long id) {
        //do some custom logic here
        //then delete the sample
        //sampleRepository.delete(id);

    }

However if now try to make a GET api/samples/1(someId) or look up some search functionality on the RepositoryRestResource, I see the following error

"description": "Request method 'GET' not supported"

is there way to override only one HTTP verb have the rest of the functionality coming up from the repository.

However if i comment public void delete from the controller i am able to access all the crud and Search operations

Has Anyone encountered such an issue

I am using SPRING_DATA_REST-2.5.1-Release

like image 242
Raghu Chaitanya Avatar asked May 06 '16 19:05

Raghu Chaitanya


People also ask

What is difference between JpaRepository and CrudRepository?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Why is Spring Data rest not recommended in real world applications?

It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.

What is @RepositoryRestController?

@RepositoryRestResourceIf you want to write a custom handler for a specific resource taking advantage of Spring Data REST's settings, message converters, exception handling and more, you can use @RepositoryRestController (instead of the standard Spring MVC @Controller or @RestController annotations).

What is collectionResourceRel?

and collectionResourceRel is described: The rel value to use when generating links to the collection resource.


2 Answers

You need to define your controller as

@RepositoryRestController
public class SampleController{
    @Autowired
    SampleRepository sampleRepository;

    @RequestMapping(value = "/api/samples/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable Long id) {

    }

As well as spring data provide different events to perform before and after the domain create,save and delete.

Refer http://docs.spring.io/spring-data/rest/docs/current/reference/html/#events

like image 97
NIrav Modi Avatar answered Sep 20 '22 10:09

NIrav Modi


You have to use the RequestMapping annotation at the method level only.

like image 23
Marc Tarin Avatar answered Sep 22 '22 10:09

Marc Tarin