不允许删除 CrudRepository @RequestMapping

CrudRepository delete not allowed @RequestMapping

我直接公开了 CustomRepository 中使用的 @RequestMapping 的存储库方法。 CrudRepository 提供的默认方法适用于 @RequestMapping 除了 delete(ID id)

给出下面的代码

@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);
}

当我们 运行 使用 spring 启动时它抛出一个错误。 创建 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

这是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

因此,为了自定义删除方法,您需要创建一个控制器,例如 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);
    }
}

并从 IReportRepository 中删除 delete 方法:

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

}

CrudRepository 已经在此表单上公开了删除方法

  • void delete(ID id) - (ID extends Serializable)
  • 无效删除(T实体)
  • void deleteAll()

所以定义void delete(@PathVariable Integer id);会报错。

定义另一个签名的一种方法是包装在另一个休息控制器中