5

当我调用 URL 时,http://192.168.2.26:8080/rest/RestSample/season/1.json我收到错误:

"Error","ajp-bio-8012-exec-4","03/01/13","16:51:58","RestSample","object is not an instance of declaring class 文件的具体顺序包含或处理的是: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

应用程序.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>

服务.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
4

2 回答 2

3

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

转到 ColdFusion 管理员中的 REST 服务并删除任何现有的 REST 注册。

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

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”。

如果您仍有问题,请告诉我,我们会看看我们能做些什么。

于 2013-03-02T00:04:56.180 回答
1

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

于 2013-03-28T15:14:35.883 回答