简体   繁体   English

CrudRepository不允许删除@RequestMapping

[英]CrudRepository delete not allowed @RequestMapping

I have directly exposed repository methods with @RequestMapping used in CustomRepository . 我直接在CustomRepository使用@RequestMapping公开了存储库方法。 Default method provided by CrudRepository work fine with @RequestMapping except delete(ID id) . 除了delete(ID id)之外, CrudRepository提供的默认方法可以与@RequestMapping CrudRepository正常工作。

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. 当我们使用spring boot运行它时,它抛出一个错误。 Logs print ambiguous method delete while create bean. 创建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

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 要利用Spring Data REST的设置,消息转换器,异常处理等功能,请使用@RepositoryRestController批注,而不是标准的Spring MVC @Controller或@RestController

So, in order to customize the delete method, you need to create a controller for example ReportRepositoryController: 因此,为了自定义delete方法,您需要创建一个控制器,例如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: 并从IReportRepository中delete方法:

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

}

CrudRepository already exposes delete methods on this form CrudRepository已经在此表单上公开了delete方法

  • void delete(ID id) - (ID extends Serializable) 无效delete(ID id)-(ID扩​​展可序列化)
  • void delete(T entity) 无效delete(T实体)
  • void deleteAll() 无效deleteAll()

so if you define void delete(@PathVariable Integer id); 因此,如果您定义void delete(@PathVariable Integer id); it will report error. 它将报告错误。

One way defining another signature would be wrap inside another rest controller 定义另一个签名的一种方式是包装在另一个rest控制器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM