Coldfusion:跨ACF11 / Lucee 4.5 RestInitApplication()解决方案

时间:2018-08-08 12:12:27

标签: coldfusion coldfusion-11 lucee

当我使用以下代码时:

<cftry>
    <cfif CompareNoCase(request.engine,"Railo") EQ 0>
        <cfset RestInitApplication(request.filepath & "\com\api",local.serviceMapping,false,request.webAdminPassword) />
    <cfelse>
        <cfset RestInitApplication(request.filepath & "\com\api",local.serviceMapping) />
    </cfif>
    <cfcatch>
     </cfcatch>
</cftry>

Adob​​e Coldfusion引发以下编译错误:

Parameter validation error for the RESTINITAPPLICATION function.
The function accepts 1 to 3 parameters. 

任何人都可以提出一种方法来阻止Coldfusion抛出错误吗?

如果我尝试执行以下操作:

<cfif CompareNoCase(request.engine,"Railo") EQ 0>
    <cfset RestInitApplication(dirPath=request.filepath & "\com\api",serviceMapping=local.serviceMapping,password=request.webAdminPassword) />
<cfelse>
    <cfset RestInitApplication( dirPath=request.filepath & "\com\api", serviceMapping=local.serviceMapping) />
</cfif>

ACF引发另一种编译错误:

Parameter validation error for the RESTINITAPPLICATION function.
A built-in ColdFusion function cannot accept an assignment statement as a parameter, although it can accept expressions. For example, RESTINITAPPLICATION(d=a*b) is not acceptable. 

如果我尝试:

<cfif CompareNoCase(request.engine,"Railo") EQ 0>
  <cfset this.webAdminPassword = request.webAdminPassword />
  <cfset RestInitApplication(request.filepath & "\com\api",local.serviceMapping) />
<cfelse>
  <cfset RestInitApplication(request.filepath & "\com\api",local.serviceMapping) />
</cfif>

Lucee抛出一个错误,告诉我密码未定义。

环境:

Windows 10上的ACF11

Windows 2008R2服务器上的Lucee 4.5

1 个答案:

答案 0 :(得分:4)

好。我找到了官方支持的解决方案。如果您查看Adobe Coldfusion官方文档,页面底部的标题为“站点级REST应用程序支持”的段落:

https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html

它说明您可以使用:

this.restsettings

几乎所有:

RestInitApplication()

参数。

我尝试了未提及的内容,例如:

dirPath
password

它有效。

这是解决方案:

<cfif CompareNoCase(request.engine,"Railo") EQ 0>
  <cfset this.restsettings.dirPath = request.filepath & "\com\api" />
  <cfset this.restsettings.serviceMapping = local.serviceMapping />
  <cfset this.restsettings.password = request.webAdminPassword />
<cfelse>
  <cfset this.restsettings.dirPath = request.filepath & "\com\api" />
  <cfset this.restsettings.serviceMapping = local.serviceMapping />
</cfif>

更新1

我真诚地发现,当对REST API CFC进行更改时,需要刷新其余应用程序。

这是出于以下目的:

RestInitApplication()

因此,我设计了一种解决此问题的解决方案,实际上简化了例程。似乎 RestInitApplication()所需的唯一参数是:

dirPath

根据这些新信息,以下是我原来的回答:

<cftry>
  <cfif CompareNoCase(request.engine,"Railo") EQ 0>
    <cfset this.restsettings.password = request.webAdminPassword />
  </cfif>
  <cfset restInitApplication(local.dirPath,local.serviceMapping)>
  <cfcatch>
  </cfcatch>
</cftry>

您实际上可以删除第二个参数,并将其替换为:

<cfset this.restsettings.serviceMapping = local.serviceMapping />

但是,我将把这个决定留给您!

更新2:

与肖恩·科菲尔德(Sean Corfield)交谈后,他还提出了另一种非常聪明的方法。

使用:

<cfinclude>

作为每个 restInitApplication()

版本的容器

即使参数计数冲突仍然存在,代码在编译时也不会引发错误。将 cfinclude 放在条件子句中时,它充当缓冲区。

这是第二种解决方案,在我看来,这是最干净的一种解决方案,因为它初始化了REST API,即它的实现方式:

<cfif CompareNoCase(request.engine,"Railo") EQ 0>
  <cfset restinitdirpath = local.dirPath>
  <cfset restinitservicemapping = local.serviceMapping>
  <cfset restinitdefault = false>
  <cfset restinitpassword = request.webAdminPassword>
  <cfinclude template="includes/structures/rest/railo-rest-init-structure.cfm">
<cfelse>
  <cfset restinitdirpath = local.dirPath>
  <cfset restinitservicemapping = local.serviceMapping>
  <cfinclude template="includes/structures/rest/coldfusion-rest-init-structure.cfm">
</cfif>

railo-rest-init-structure.cfm

<cfoutput>

  <cfparam name="restinitdirpath" default="">
  <cfparam name="restinitservicemapping" default="">
  <cfparam name="restinitdefault" default="false">
  <cfparam name="restinitpassword" default="">

  <cfset restInitApplication(restinitdirpath,restinitservicemapping,restinitdefault,restinitpassword)>

</cfoutput>

coldfusion-rest-init-structure.cfm

<cfoutput>

  <cfparam name="restinitdirpath" default="">
  <cfparam name="restinitservicemapping" default="">

  <cfset restInitApplication(restinitdirpath,restinitservicemapping)>

</cfoutput>

现在,我已经对此进行了测试,并且可以正常运行,随后每次点击 restInitApplication()时,都会检测到对REST API CFC的任何更改。