2014-01-30 64 views
2

我正在玩ColdFusion 10的RESTful Web服务。首先,我通过CF管理员注册了一项休息服务。REST - 404未找到

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未找到。我在这里错过了什么?

回答

4

你犯了两个很小的错误。首先是你在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的端口已经=“8500”的参数。

重要注意事项:一旦您对CFC文件进行了任何修改,请确保您转到管理员并通过单击刷新图标重新加载您的REST服务!

为了完整起见,我在上面的代码在本地CF10工作,这里是返回的JSON:

{"COLUMNS":["ID","NAME"],"DATA":[[1,"Sagar"]]} 
+0

真棒!谢谢。 – CFNinja