Coldfusion RESTful webservice: Object is not an instance of declaring class

白昼怎懂夜的黑 提交于 2019-12-10 13:47:13

问题


When I call the URL http://192.168.2.26:8080/rest/RestSample/season/1.json I get the error:

"Error","ajp-bio-8012-exec-4","03/01/13","16:51:58","RestSample","object is not an instance of declaring class The specific sequence of files included or processed is: C:\path_to\api\service.cfc'' "

This is the /api/service.cfc file:

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

Edit #1: following this tutorial: http://www.anujgakhar.com/2012/02/20/using-rest-services-in-coldfusion-10/

Edit #2: my 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>

Also would like to note, refreshing REST services in the admin ALWAYS gives me the following message:

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.

However, I can remove them and add them via the onRequestStart() without any problems.

Edit #3

My current structure

/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>

I still get the following error:

'object is not an instance of declaring class

回答1:


Let's start a little simpler example and see if you can get to a working state. I've successfully set up a working REST service by following these steps:

Go to REST Services in your ColdFusion Administrator and remove any existing REST registrations.

In a new directory in your web root, create Application.cfc with the following content (note that the <cfscript> tags are not necessary if you're on CF 9 or above):

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

In the same directory, create index.cfm with the following content:

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

In the same directory, create service.cfc with the following content:

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

First, browse to the index.cfm through your browser and verify that you see the text 'Done.'

Open REST Services in ColdFusion Administrator and verify that you see the REST service has been registered successfully.

Finally, browse to the REST resource in your browser via /rest/RestSample/season/123 and hopefully you'll see the trusty "hello world".

Let me know if you still have troubles and we'll see what we can do.




回答2:


I created the files under C:\ColdFusion10\cfusion\wwwroot (instead of the site's IIS root) and was able to register the REST service via the administration console without any issue.



来源:https://stackoverflow.com/questions/15166822/coldfusion-restful-webservice-object-is-not-an-instance-of-declaring-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!