Coldfusion RESTful webservice:Object不是声明类的实例

时间:2013-03-01 21:01:52

标签: web-services rest coldfusion cfml

当我拨打网址http://192.168.2.26:8080/rest/RestSample/season/1.json时,我收到错误消息:

  

“错误”,“ajp-bio-8012-exec-4”,“03/01/13”,“16:51:58”,“RestSample”,“对象不是声明类的实例具体包含或处理的文件序列为:C:\ path_to \ api \ service.cfc''“

这是/api/service.cfc文件:

<cfscript>
component restpath="season" rest="true"
{

    remote query function getSeasonsByUserId(numeric userid restargsource="Path") httpmethod="GET" restpath="{userid}"
    {
        var response = "";
        var qry = new Query();
        var userQry = "";

        qry.setSQl("select * from mytable where userID = :userid");
        qry.addParam(name="userid", value="#arguments.userid#", cfsqltype="cf_sql_numeric");
        userQry = qry.execute().getResult();

        if(userQry.recordcount == 0)
        {
            response = userQry;
        } else {
            throw(type="Restsample.SeasonsNotFoundError", errorCode='404', detail='Seasons not found');
        }

        return response;
    }    
}   
</cfscript>

编辑#1 :遵循本教程: http://www.anujgakhar.com/2012/02/20/using-rest-services-in-coldfusion-10/

编辑#2 :我的application.cfc

<cfscript>
component output="false"
{
    this.name = "RestSample";
    this.applicationTimeout = createTimespan(0,0,0,0);
    this.datasource = "mydsn";
    this.username = "";
    this.password = "";

    //this.restsettings.skipCFCWithError = true;

    public boolean function onRequestStart()
    {
        restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()), this.name);

        return true;
    }
}
</cfscript>

还要注意,在管理员中刷新REST服务总是给我以下消息:

Unable to refresh REST service.
Application RestSample could not be initialized.
Reason: The application does not contain any rest enabled CFCs.
The application does not contain any rest enabled CFCs.

但是,我可以删除它们并通过onRequestStart()添加它们而没有任何问题。

编辑#3

我目前的结构

/api/main/service.cfc /api/application.cfc /api/index.cfm

的Application.cfc

<cfscript>
component output="false"
{
    this.name = "RestSample";
    this.applicationTimeout = createTimespan(0,0,0,0);
    this.datasource = "mydsn";
    this.username = "";
    this.password = "";

    this.restsettings.skipCFCWithError = true;

    public boolean function onRequestStart()
    {
        restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()).concat("main\"), this.name);

        return true;
    }
}
</cfscript>

service.cfc

<cfscript>
component restpath="season" rest="true"
{

    remote Query function getSeasonsByUserId(numeric userid restargsource="Path") httpmethod="GET" restpath="{userid}"
    {
        var response = "";
        var qry = new Query();
        var userQry = "";

        qry.setSQl("select * from mytable where userID = :userid");
        qry.addParam(name="userid", value="#arguments.userid#", cfsqltype="cf_sql_numeric");
        userQry = qry.execute().getResult();

        return userQry;
    } 
}   
</cfscript>

我仍然收到以下错误:

'object is not an instance of declaring class

2 个答案:

答案 0 :(得分:3)

让我们开始一个更简单的例子,看看你是否可以进入工作状态。我已按照以下步骤成功设置了有效的REST服务:

转到ColdFusion Administrator中的REST服务并删除所有现有的REST注册。

在您的网络根目录的新目录中,使用以下内容创建Application.cfc(请注意,如果您使用的是CF 9或更高版本,则不需要<cfscript>标记):

component output="false"
{
    this.name = "RestSample";
}

在同一目录中,使用以下内容创建index.cfm:

<cfset restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()), "RestSample") />
Done.

在同一目录中,使用以下内容创建service.cfc:

component restpath="season" rest="true"
{
    remote struct function getSeasonsByUserId(numeric userid restargsource="Path") httpmethod="GET" restpath="{userid}"
    {
        return {
            'hello': 'world'    
        };
    } 
}

首先,通过浏览器浏览index.cfm并确认您看到文本“完成”。

在ColdFusion Administrator中打开REST服务,并验证您是否已成功注册REST服务。

最后,通过/ rest / RestSample / season / 123浏览浏览器中的REST资源,希望您能看到可靠的“hello world”。

如果您仍然遇到麻烦,请告诉我们,我们会看到我们能做些什么。

答案 1 :(得分:1)

我在C:\ColdFusion10\cfusion\wwwroot下创建了文件(而不是网站的IIS根目录),并且能够通过管理控制台注册REST服务而没有任何问题。