本文介绍了REST-找不到404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ColdFusion 10的RESTful Web服务。首先,我通过CF管理员注册了休息服务。

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

C:/ ColdFusion10 / cfusion / wwwroot / restful /并称为IIT。

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

现在,我有C:/ColdFusion10/cfusion/wwwroot/restful/restTest.cfc,它是:

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>

我还创建了C:/ColdFusion10/cfusion/wwwroot/restful/callTest.cfm,其内容如下:

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

当我运行callTest.cfm时,我得到404 Not Found。我在这里错过了什么?

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

推荐答案

您正在犯两个非常小的错误。首先是您要在CFC中提供restpath = IIT,但是随后尝试在URL中使用 restTest。使用restpath = IIT时,URL为 IIT / IIT,而不是 IIT / restTest。如果要在URL中使用 IIT / restTest,则应为以下组件定义:

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>

您犯的第二个错误是您的CFHTTP调用包含了方法的名称。这不是使用CF Rest服务的方法。这是调用CFM文件的正确方法:

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

此外,当您在URL中指定端口时,我放弃了port = 8500参数

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

重要说明:对CFC文件进行任何修改后,请确保您进入管理员并通过单击重新加载REST服务。

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!

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

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

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

这篇关于REST-找不到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:00