【发布时间】: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