首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >冲突方法签名

冲突方法签名
EN

Stack Overflow用户
提问于 2018-10-26 08:37:21
回答 1查看 60关注 0票数 0

我正在尝试创建一个带有两个save方法的rest控制器。一个用于保存单个实体,另一个用于保存实体列表。

代码语言:javascript
复制
@RequestMapping(method = [POST, PUT])
fun save(@RequestBody entity: T): T {
    return service.save(entity)
}

@RequestMapping(method = [POST, PUT])
fun save(@RequestBody entities: List<T>): List<T> {
    return service.save(entities)
}

然而,由于我假设的是,类型擦除spring抛出了以下异常。

代码语言:javascript
复制
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'crudControllerImpl' method 
public final T dk.fitfit.send.mail.CrudController.save(T)
to {[/messages],methods=[POST || PUT]}: There is already 'crudControllerImpl' bean method
public final java.util.List<T> dk.fitfit.send.mail.CrudController.save2(java.util.List<? extends T>) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
...

有什么线索吗?

EN

Stack Overflow用户

发布于 2018-10-26 14:10:30

这是因为您有相同的请求映射。Spring不能根据请求负载来区分请求--只能区分方法、路径和头部。你可以尝试从这里应用这个建议:Jackson mapping Object or list of Object depending on json input

对于您的情况,它将看起来像

代码语言:javascript
复制
@RequestMapping(method = [POST, PUT])
fun save(@RequestBody @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) entities: List<T>): List<T> {
    return service.save(entities)
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52999870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档