2014-01-30 136 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