【问题标题】:REST - 404 Not FoundREST - 404 未找到
【发布时间】:2014-01-30 02:53:21
【问题描述】:

我正在使用 ColdFusion 10 的 RESTful Web 服务。首先,我通过 CF admin 注册了一个 rest 服务。

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

【问题讨论】:

    标签: rest coldfusion


    【解决方案1】:

    你犯了两个非常小的错误。首先是您在 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#" />
    

    另外,我删除了 port="8500" 参数,因为您已经在 URL 中指定了端口。

    重要提示:对 CFC 文件进行任何修改后,请确保转到管理员并通过单击刷新图标重新加载 REST 服务!

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多