10

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

Raghu Chaitanya
  • 151
  • 2
  • 8
  • not exactly a duplicate, but the solution is the same: http://stackoverflow.com/questions/21734149/namedquery-override-findall-in-spring-data-rest-jparepository – WeMakeSoftware May 06 '16 at 19:29

2 Answers2

8

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

NIrav Modi
  • 6,038
  • 8
  • 32
  • 47
  • @Nlrav Modi I thought we use the events t do some kind of entity validations and check if the bean meets some criteria – Raghu Chaitanya May 08 '16 at 17:38
  • yes, but I don't know what is your custom logic, So I had given another solution about an event. You can accept the answer if your problem solved. – NIrav Modi May 09 '16 at 03:41
7

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

Marc Tarin
  • 3,109
  • 17
  • 49
  • Thanks for pointing Out the link [link] http://docs.spring.io/spring-data/rest/docs/2.4.0.M1/reference/html/#customizing-sdr.overriding-sdr-response-handlers. This Resolved it – Raghu Chaitanya May 10 '16 at 16:50