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

标签 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>

Adobe 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

最佳答案

好的。我找到了一个官方支持的解决方案。如果您查看 Adob​​e 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 所做的任何更改。

关于Coldfusion:跨ACF11/Lucee 4.5 RestInitApplication()解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746453/

相关文章:

coldfusion - 具有特殊字符的查询中的访问列?

mysql - 创建表 tuserremotesessions 安装错误

email - 使用 CFMAIL 发送时显示完整的 "from"名称

web-services - ColdFusion Lucee 或 Railo RestFull Web 服务映射问题

coldfusion - 组件上 accessors=true 的意外行为

railo - server.railo 是否存在于 Lucee 实例上?

coldfusion - Railo web.cfm 显示 IIS 404

javascript - 清除更改功能的表单字段会删除表单发布值

hibernate - CFML 使用哪个版本的 Hibernate?

coldfusion - 元素 VAR 在 ATTRIBUTES ColdFusion 11 中未定义