Karan Verma
Karan Verma

Reputation: 69

CrudRepository delete not allowed @RequestMapping

I have directly exposed repository methods with @RequestMapping used in CustomRepository. Default method provided by CrudRepository work fine with @RequestMapping except delete(ID id).

Given code below

@RepositoryRestResource(path = "/ces/data/reports")
@RequestMapping("/ces/data/reports")
@Api(value="reports")
public interface IReportRepository<S> extends CrudRepository<Report,Integer> {
    @CrossOrigin
    @RequestMapping(path="/delete/{id}",method = RequestMethod.GET)
    void delete(@PathVariable Integer id);
}

It throws an error when we run this with spring boot. Logs print ambiguous method delete while create bean.

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'IReportRepository' method
public default void com.pb.ces.emessaging.mvp.web.repository.IReportRepository.delete(java.io.Serializable)
to {[/ces/data/reports/delete/{id}],methods=[GET]}: There is already 'IReportRepository' bean method
public abstract void com.pb.ces.emessaging.mvp.web.repository.IReportRepository.delete(java.lang.Integer) mapped.
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:567)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:531)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:255)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:241)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:213)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:183)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:125)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        ... 50 more

Upvotes: 1

Views: 2186

Answers (2)

kuhajeyan
kuhajeyan

Reputation: 11017

CrudRepository already exposes delete methods on this form

  • void delete(ID id) - (ID extends Serializable)
  • void delete(T entity)
  • void deleteAll()

so if you define void delete(@PathVariable Integer id); it will report error.

One way defining another signature would be wrap inside another rest controller

Upvotes: 1

eparvan
eparvan

Reputation: 1729

Here is the docs

Sometimes you may want to write a custom handler for a specific resource. To take advantage of Spring Data REST’s settings, message converters, exception handling, and more, use the @RepositoryRestController annotation instead of a standard Spring MVC @Controller or @RestController

So, in order to customize the delete method, you need to create a controller for example ReportRepositoryController:

@RepositoryRestController
public class ReportRepositoryController {

    @Inject
    private IReportRepository repository;

    @RequestMapping(method = RequestMethod.GET, value = "/ces/data/reports/delete/{id}")
    @ResponseBody
    public ResponseEntity<?> deleteReport(@PathVariable Long id) {
        repository.delete(id);
        return ResponseEntity.ok(HttpStatus.NO_CONTENT);
    }
}

And to remove the delete method from IReportRepository:

@RepositoryRestResource(path = "/ces/data/reports")
@Api(value="reports")
public interface IReportRepository<S> extends CrudRepository<Report,Integer> {

}

Upvotes: 1

Related Questions