rest - grails 3.3.9-从restfulController扩展失败,出现 “No default constructor found”

标签 rest grails controller

使用Grails v3.3.9。

访问稳定的 Controller 时出错。 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")

我的 Controller 像这样扩展了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扩展的解决办法是什么?

最佳答案

my controller extends RestfulController like this, which wont permit a default constructor to be added to the class



那是不对的。您的构造函数不应接受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创建 Controller 实例时,它不会将任何内容传递给构造函数,因此您需要no-arg构造函数。

关于rest - grails 3.3.9-从restfulController扩展失败,出现 “No default constructor found”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54235373/

相关文章:

asp.net - Swagger 忽略 xml 参数标签

包含在网页中的 Java 报告库

grails - Grails删除 child

hibernate - 使用 GORM/Hibernate 的预加载查询

JavaFX : can controller be an abstract class?

ruby-on-rails - 如何在 AuthLogic Controller 规范中使用模拟模型?

controller - 从另一个 Controller 调用另一个模块中的 Controller

java - 如何获取 Jenkins 服务器上 findbugs 生成的报告?

entity-framework - 如何从外部安逸的服务中填充电灯开关屏幕?

python - Django REST 框架 - 多个查找字段?