我正在使用ColdFusion 10的RESTful Web服务。首先,我通过CF管理员注册了休息服务。
C:/ ColdFusion10 / cfusion / wwwroot / restful /并称之为IIT。
现在,我有C:/ColdFusion10/cfusion/wwwroot/restful/restTest.cfc,这是:
<cfcomponent restpath="IIT" rest="true" >
<!--- handle GET request (httpmethod), take argument in restpath(restpath={customerID}), return query data in json format(produces=text/json) --->
<cffunction name="getHandlerJSON" access="remote" httpmethod="GET" restpath="{customerID}" returntype="query" produces="application/json">
<cfargument name="customerID" required="true" restargsource="Path" type="numeric">
<cfset myQuery = queryNew("id,name", "Integer,varchar", [[1, "Sagar"], [2, "Ganatra"]])>
<cfquery dbtype="query" name="resultQuery">
select * from myQuery
where id = #arguments.customerID#
</cfquery>
<cfreturn resultQuery>
</cffunction>
</cfcomponent>
我还创建了C:/ColdFusion10/cfusion/wwwroot/restful/callTest.cfm,其中包含以下内容:
<cfhttp url="http://127.0.0.1:8500/rest/IIT/restTest/getHandlerJSON/1" method="get" port="8500" result="res">
<cfhttpparam type="header" name="Accept" value="application/json">
</cfhttp>
<cfdump var="#res#">
当我运行callTest.cfm时,我得到404 Not Found。我在这里缺少什么?
答案 0 :(得分:5)
你犯了两个非常小的错误。第一个是你在CFC中提供restpath =“IIT”,但是然后尝试在URL中使用“restTest”。使用restpath =“IIT”,URL将是“IIT / IIT”,而不是“IIT / restTest”。如果您想在URL中使用“IIT / restTest”,那么组件定义应该是什么:
<cfcomponent restpath="restTest" rest="true" >
<!--- handle GET request (httpmethod), take argument in restpath(restpath={customerID}), return query data in json format(produces=text/json) --->
<cffunction name="getHandlerJSON" access="remote" httpmethod="GET" restpath="{customerID}" returntype="query" produces="application/json">
<cfargument name="customerID" required="true" restargsource="Path" type="numeric">
<cfset myQuery = queryNew("id,name", "Integer,varchar", [[1, "Sagar"], [2, "Ganatra"]])>
<cfquery dbtype="query" name="resultQuery">
select * from myQuery
where id = #arguments.customerID#
</cfquery>
<cfreturn resultQuery>
</cffunction>
</cfcomponent>
您犯的第二个错误是您的CFHTTP调用包含方法的名称。这不是如何使用CF休息服务。以下是调用CFM文件的正确方法:
<cfhttp url="http://127.0.0.1:8500/rest/IIT/restTest/1" method="get" result="res">
<cfhttpparam type="header" name="Accept" value="application/json">
</cfhttp>
<cfdump var="#res#" />
此外,我在URL中指定了端口时,删除了port =“8500”参数。
重要提示:对CFC文件进行任何修改后,请确保转到管理员并通过单击“刷新”图标重新加载REST服务!
为了完整起见,我将上面的代码在CF10上本地工作,这里是返回的JSON:
{"COLUMNS":["ID","NAME"],"DATA":[[1,"Sagar"]]}