【问题标题】:Coldfusion RESTful webservice: Object is not an instance of declaring classColdfusion RESTful webservice:对象不是声明类的实例
【发布时间】:2013-03-01 21:01:52
【问题描述】:

当我调用 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

【问题讨论】:

  • 我还没有使用过 CF 的 REST 实现,但应该是 RestSetResponse 吗?
  • @PeterBoughton 我不这么认为......我的映射是 RestSample,因此它也应该是 RestSample?老实说,我正在关注这个教程。
  • 诊断问题的第一步是确定错误的来源。删除(或注释掉) if 并更改返回到 return userQry; 以查看这是否是错误所在。 (附注:== 0 是从前到后的,无论如何都是不必要的。)
  • 在问题中包含指向教程的链接 - 这样可以检查您是否误解了说明或教程是否有误。
  • @PeterBoughton 我去掉了 if 语句,并返回了查询(在从管理控制台中删除了其余服务并重新添加之后)......同样的错误。

标签: web-services rest coldfusion cfml


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 另请注意(如您正在学习的教程的 cmets 中所讨论的),您必须创建一个 index.cfm 以点击以刷新 REST 服务。
  • 我有 index.cfm 并应用了您的修复程序。我仍然遇到同样的问题。
  • 我复制了你的文件,这是我得到的:"Error","ajp-bio-8012-exec-10","03/06/13","13:01:51 ",,"'' 包含或处理的文件的具体顺序是:'''' "
  • 哇。我对你完成这项工作的决心印象深刻。如果我是你,我现在就会拿起太妃糖了。我不知道你得到的错误是什么意思。你什么时候收到这个错误,它是如何出现的?您是否查看了错误日志以查看是否可以找到更多详细信息?自从遇到问题后,您是否重新启动了 CF?
  • 我在 Win7 Pro 上尝试了这个设置,单独使用 CF10 并且它可以工作。我想知道为什么它在我的虚拟机上不起作用。同时,我只是创建了一个输出 JSON 的页面,并且我有需要它的进程使用它来代替。我还没有重新启动,我会在今天晚些时候尝试。日志只显示我提交的内容,仅此而已。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 2017-05-05
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2019-07-03
    相关资源
    最近更新 更多