grails 3.3.9-从restfulController扩展失败,并显示“未找到默认构造函数”

时间:2019-01-17 11:53:06

标签: rest grails controller

使用grails v3.3.9。

访问静态控制器时出错。 grails网站显示了

URI
/api/device
Class
java.lang.NoSuchMethodException
Message
Error creating bean with name 'com.softwood.controller.DeviceController': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.softwood.controller.DeviceController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.softwood.controller.DeviceController.<init>()
Caused by
com.softwood.controller.DeviceController.<init>()

我已经像这样设置了UrlMapping

    get "/api/device"(controller:"device", action:"index")

我的控制器像这样扩展了RestfulController,这不允许将默认构造函数添加到类

class DeviceController extends RestfulController<Device> {
    static responseFormats = ['json', 'xml']

    //static scaffold = Device

    DeviceController(Class<Device> device) {
        this(device, false)
    }

    DeviceController(Class<Device> device, boolean readOnly) {
        super(device, readOnly)
    }

    def index (Integer max) {
        params.max = Math.min(max ?: 10, 100)
        Collection<Device> results = Device.list(sort:"name")
        respond results, deviceCount: Device.count()

    }

    def show (Device device) {
        if(device == null) {
            render status:404
        } else {respond device}
    }
}

这里有一个与Extending RestfulController for a base SubClassRestfulController is not working on grails 3.0.4相关的链接

但是我已经清理了构建,重新运行等等,但是没有任何效果。实例化同样失败

此功能允许从RestfulController扩展吗?

1 个答案:

答案 0 :(得分:2)

  

我的控制器像这样扩展RestfulController,这将不允许   要添加到类中的默认构造函数

那是不对的。您的构造函数不应接受Class作为参数。您需要没有参数的构造函数。

class DeviceController extends RestfulController<Device> {
    static responseFormats = ['json', 'xml']

    DeviceController() {
        super(Device)

        /* If you want it to be read only, use super(Device, true) 
           instead of super(Device)
         */
    }

    // ...
}

Spring创建控制器实例时,不会将任何东西传递给构造函数,因此您需要no-arg构造函数。