子控制器上的重载方法映射上的方法

时间:2019-05-03 06:45:34

标签: spring-boot spring-mvc controller overloading

我试图用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扩展其他方法,但是在创建的情况下,我还有其他附加参数

1 个答案:

答案 0 :(得分:0)

请参见answer

  

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

如答案中所述,您无法在尝试实现时扩展控制器。