REST - 404 Not Found

2020-03-25 11:31发布

问题:

I am playing with ColdFusion 10's RESTful web services. First, I registered a rest service via CF admin.

C:/ColdFusion10/cfusion/wwwroot/restful/ and called it IIT.

Now, I have C:/ColdFusion10/cfusion/wwwroot/restful/restTest.cfc which is:

<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>

I also created C:/ColdFusion10/cfusion/wwwroot/restful/callTest.cfm which has the following:

<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#">

When I run callTest.cfm, I am getting 404 Not Found. What am I missing here?

回答1:

You are making two very minor mistakes. The first is that you are providing the restpath="IIT" in the CFC, but are then trying to use "restTest" in the URL. With restpath="IIT", the URL would be "IIT/IIT", not "IIT/restTest". Here is what the component definition should be if you are wanting to use "IIT/restTest" in the URL:

<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>  

The second mistake you are making is that your CFHTTP call is including the name of the method. This is not how to use CF rest services. Here is the proper way to call in your CFM file:

<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#" />

Also, I dropped off the port="8500" parameter as you specify the port in the URL already.

Important note: Once you make any modification to the CFC file, make sure you go to the Administrator and reload your REST service by clicking on the Refresh icon!

For completeness, I have the above code working locally on CF10, and here is the returned JSON:

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