【问题标题】:Overloading method on child controller method on mapping子控制器上的重载方法映射上的方法
【发布时间】:2019-06-13 23:04:05
【问题描述】:

我尝试用 Spring boot 创建 RestApi 服务

我有父类

public abstract class GenericRestController{

   @RequestMapping(
            produces = MediaType.APPLICATION_JSON_VALUE,
            method = RequestMethod.GET,
            path = "/list"
    )
    @ApiOperation("Get all entities")
    public SimpleResponse create(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size) {
..somehting
}

}

 @RestController 
 @Api("Parameters") 
 @RequestMapping(value = Controller.PARAMETERS) 
 public class ParameterController extends
 GenericRestController {

     @RequestMapping(
             produces = MediaType.APPLICATION_JSON_VALUE,
             method = RequestMethod.POST,
             consumes = MediaType.APPLICATION_JSON_VALUE,

     )
     @ApiOperation("Create new entity")
     public SimpleResponse create(@RequestBody Parameter parameter,
                                  @PathVariable(value = "rate-id") Long rateId) { ...something
     }

 }

我得到错误

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'parameterController' method 
public kz.sanscrit.cbs.common.pojo.SimpleResponse kz.sanscrit.cbs.rates.controllers.GenericRestController.create(E)
to {[/rates/{rate-id}/parameters],methods=[POST],consumes=[application/json],produces=[application/json]}: There is already 'parameterController' bean method

我如何正确重载这个方法,因为我需要从这个 GenericClass 扩展其他方法,但是在创建的情况下我还有其他附加参数

【问题讨论】:

    标签: spring-boot spring-mvc controller overloading


    【解决方案1】:

    answer

    一个@Controller-annotated 类不应扩展另一个@Controller-annotated 类,因为父类的方法也存在于子类中。

    如答案中所述,您无法按照您的目标扩展控制器。

    【讨论】:

      猜你喜欢
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多