Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 752
TechFanDan Avatar asked Oct 04 '22 20:10

TechFanDan


2 Answers

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.

like image 153
Honey Avatar answered Oct 13 '22 09:10

Honey


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.

like image 42
TechFanDan Avatar answered Oct 13 '22 09:10

TechFanDan