不允许删除CrudRepository @RequestMapping

时间:2016-11-07 08:17:38

标签: java spring spring-boot spring-data-jpa spring-hateoas

我直接使用@RequestMapping中使用的CustomRepository公开了存储库方法。 CrudRepository提供的默认方法与@RequestMappingdelete(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 boot运行它时会抛出错误。 创建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

2 个答案:

答案 0 :(得分:1)

以下是docs

  

有时您可能希望为特定资源编写自定义处理程序。要利用Spring Data REST的设置,消息转换器,异常处理等,请使用@RepositoryRestController注释而不是标准的Spring MVC @Controller或@RestController

因此,为了自定义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);
    }
}

并从IReportRepository中删除delete方法:

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

}

答案 1 :(得分:1)

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

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

因此,如果您定义void delete(@PathVariable Integer id);,则会报告错误。

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