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