Extending RestfulController for a base SubClassRes

2020-02-16 04:53发布

According to Extending Restful Controller

I Can create a SubClass of this RestfulController to become a Base controller class for every rest Controller I want.

Its OK subclassing this to a DIRECT resource controller.

Class BooksController extends RestfulController {
BooksController() {super(Book)}

But if instead I follow the grails DOC to create a base controller like this:

class MyRestController<T> extends RestfulController<T> {
static responseFormats = ['json', 'xml']
MyRestController(Class<T> domainClass) {this(domainClass, false)}
MyRestController(Class<T> domainClass, boolean readOnly) {
    super(domainClass, readOnly)
}

I got an error:

Failed to instantiate [clash.MyRestController]: No default constructor found;

I tried putting this controller inside controllers folder or src folder.

Only works if I create a default constructor but it is not what I want. I don want this class to serve API for any domain class but instead I want to create new controllers extending this class.

class BooksController extends MyRestController {
BooksController() {super(Book)}

2条回答
beautiful°
2楼-- · 2020-02-16 05:01

Sorry. Just clean and run-app solved the problem. My fault

查看更多
Melony?
3楼-- · 2020-02-16 05:16

The problem is that the class is being recognized as a controller and the framework is attempting to create an instance of the controller. One of the criteria we use to identify if that should happen is we check to see if the class is abstract. The class in question should be marked abstract

One way to make it work is to move it out of the grails-app/controllers directory and define it under src/main/groovy/.

查看更多
登录 后发表回答