gpt4 book ai didi

java - CrudRepository 不允许删除 @RequestMapping

转载 作者:行者123 更新时间:2023-11-30 02:44:57 24 4
gpt4 key购买 nike

我已经使用 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 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

最佳答案

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

因此,为了自定义删除方法,您需要创建一个 Controller ,例如 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> {

}

关于java - CrudRepository 不允许删除 @RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com