Grails RESTful API插件 - 错误的服务siginiture

时间:2014-05-16 14:17:18

标签: rest grails service

我正在尝试使用Restful API插件(Restful API plugin)。 (使用Grails 2.3.8,Groovy 2.1)

如文档中所述,我创建了一个实现RestfulServiceAdapter的Grails服务。

import net.hedtech.restfulapi.RestfulServiceAdapter
import com.game.trivia.Question
@Transactional
class QuestionService implements RestfulServiceAdapter {
    @Override
    public Object list(def service, Map params) throws Throwable{
        List Q = Question.list(params)

        return Q;
    }
.  
.  
.  

尝试访问服务时:http://localhost:8080/test_triv/api/questions

我收到以下例外:

    {"errors":[{"type":"general",
    "errorMessage":"No signature of method: test_triv.QuestionService.list() is applicable for argument types:
     (org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap) values:
     [[pluralizedResourceName:questions, action:[...], ...]]\nPossible solutions: list(java.lang.Object, java.util.Map), 
    is(java.lang.Object), wait(), find(), wait(long), with(groovy.lang.Closure)"}]}

所以我实现了另一个列表方法(不是接口的一部分):

public Object list(Map params) throws Throwable {
            List Q = Question.list(params)
            return Q;
        }

哪个有效。

我做错了吗? 我是否实施了正确的界面? 我是否必须为每个域公开服务,或者有什么方法可以使用现有的控制器而不是服务? 创建新服务是一个很大的开销!我已经拥有所有域名的控制器。

1 个答案:

答案 0 :(得分:0)

刚从Charlie(The pludin开发者)那里得到了关于这个问题的答复:

我们的文档在这方面应该更加清晰,所以我会采取行动来改进它。

您不应在服务中实现RestfulServiceAdapter,但如果需要调整不提供预期方法的现有服务,则应实现并注册实现此接口的适配器。

由于您正在编写新服务,因此您可以公开所需的方法(您不需要实现任何接口)。请注意,契约与适配器接口基本相同,没有表示适配器将委派给的服务的“service”参数。

为避免需要适配器,服务应公开这些方法:

def list( Map params ) throws Throwable { ... }

def count( Map params ) throws Throwable { ... }

def show( Map params ) throws Throwable { ... }

def create( Map content, Map params ) throws Throwable { ... }

def update( def id, Map content, Map params ) throws Throwable { ... }

void delete( def id, Map content, Map params ) throws Throwable { ... }

控制器旨在委托包含业务逻辑的服务,并且不能委托给另一个控制器。我们期望RestfulApiController和应用程序中的其他控制器将共享服务(例如,ThingController和RESTfulApiController都可以使用相同的ThingService,以便不重复业务逻辑。)

相关问题