Coldfusion REST API 作成時の基本構成

cf

目次

構成

コンポーネント本体

例:main.cfc

restはture、restpathは、cfc階層パスを記載し、最後はcfcファイル名の拡張子抜きで記載。つまり、rest/api/main.cfcの場合は、/rest/api/mainとなる。

<cfcomponent rest="true" restpath="/rest/api/main" name="main">
<!--- コンポ―ネント作成部は通常Application.cfcに記載 --->
<cffunction name="setValue" access="remote" returntype="String" httpmethod="GET">
<cfargument name="ArguValue" type="string" required="yes">

    <cfif ArguValue neq "">
        <cfset status = "200">
	<cfset message = "success">
        <cfset TestValue = #ArguValue#>
    <cfelse>
        <cfset status = "500">
        <cfset message = "error">
        <cfset TestValue = "">
    </cfif>
    <!--- 処理結果をJSONで返す --->
    <cfset data = StructNew()>
    <cfset data.status = status>
    <cfset data.message = message>
    <cfset data.value = TestValue>

    <cfreturn SerializeJSON(data)>		
    </cffunction>
</cfcomponent>

コンポーネント生成部

例:application.cfc

<cfset login = CreateObject("component","rest.api.main")>
CreateObject("component","cfc保管フォルダ.cfcの拡張子無しファイル名")>

API呼出し部 ※実際にAPIとして外部に公開するファイル

例:index.cfm

#main.setValue(ArguValue)#

https://hoge.com/rest/api/

メソッド:GET

値:ArguValue: xxxxxx(string)

Facebook
Twitter
Pinterest