REST - 404 未找到 [英] REST - 404 Not Found

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

问题描述

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

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 休息服务.以下是调用 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#" />

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

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 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆