如何创建接受查询参数的 ColdFusion REST 端点

How to create a ColdFusion REST end-point which accepts query parameters

这是我尝试使用查询参数创建 REST 端点的代码片段,

<cfcomponent rest="true" restpath="api"> 
   <cffunction name="getUsersQuery" restpath="Users?filter={query}" access="remote" returntype="struct" httpmethod="GET" produces="application/json">
      <cfargument name="query" type="any" required="yes" restargsource="query" />
    
      <cfset var response = {} />
      <cfset response["message"] = "Test" />
      <cfreturn response>
  </cffunction>
</cfcomponent>

但是当我尝试像下面这样调用终点时,它给出了“Method Not Allowed”

http://localhost:8010/rest/v1/api/Users?filter=userName eq "test"

如有任何帮助,我们将不胜感激。

您不需要在其余路径中提供 ?filter={query},因为您在参数中提到了 restargsource="query"。此外,参数名称应该是 URL 键值。

<cffunction name="getUsersQuery" restpath="Users" access="remote" returntype="struct" httpmethod="GET" produces="application/json">
   <cfargument name="filter" type="any" required="yes" restargsource="query" />
    
   <cfset var response = {} />
   <cfset response["message"] = "Test" />
   <cfreturn response>
</cffunction>