2017-05-11 6 views
0

ColdFusionでWebサービスを作成する必要があります。私は以下の2つの方法を試しました。誰もが最善の方法(パフォーマンスとセキュリティ強化のベースの両方)である1を見つけるために私を助けることができるCFでwebserviceを実装するための最良の方法

最初の方法

以下のようにCFMページを作成しました。

<cfset result    = StructNew() /> 
    <cfset resultStruct   = StructNew() /> 
    <cfset validStruct   = StructNew() /> 
    <cfset VARIABLES.Sample  = CreateObject("component","main.webservice.Sample")> 

    <cfif NOT isDefined("URL.method")> 
     <cfset result['status'] = false > 
     <cfset result['message'] = 'method is missing' /> 
     <cfoutput>#SerializeJSON(result)#</cfoutput> 
     <cfabort> 
    </cfif> 

    <cfswitch expression="#URL.method#"> 
     <cfcase value="get"> 
     <cfset fieldList = "name"> 
     <cfset validStruct = validate(fieldList) /> 
     <cfif validStruct['status']> 
      <cfset resultStruct = VARIABLES.Sample.get(argumentCollection=URL) /> 
     </cfif> 
     <cfoutput>#SerializeJSON(resultStruct)#</cfoutput> 
     <cfbreak> 
     </cfcase> 

     <cfcase value="put"> 
     <cfset fieldList = "name,value"> 
     <cfset validStruct = validate(fieldList) /> 
     <cfif validStruct['status']> 
      <cfset resultStruct = VARIABLES.Sample.put(argumentCollection=URL) /> 
     </cfif> 
     <cfoutput>#SerializeJSON(resultStruct)#</cfoutput> 
     <cfbreak> 
    </cfcase> 

    <cfdefaultcase> 
     <cfset result['status'] = false > 
     <cfset result['message'] = 'Not a valid method' /> 
     <cfoutput>#SerializeJSON(result)#</cfoutput> 
     <cfbreak> 
    </cfdefaultcase> 
    </cfswitch> 

そして、上記のようにwebserviceフォルダの下に「Sample」という名前のcfcを作成しました。

のWebService URL

http://test.com/webservice/Sample.cfm?method=get&name=test

第二の方法

CFCサンプルから直接呼び出す

Sample.CFC

<cfcomponent displayname="Sample" hint="Sample WebService" output="false"> 

    <cffunction name="get" access="remote" returntype="struct" returnformat="json"> 
    <cfargument name="name" required="true" type="string" > 

    <cfreturn StructNew() />   
    </cffunction> 

    <cffunction name="put" access="remote" returntype="struct" returnformat="json"> 
    <cfargument name="name" required="true" type="string" > 
    <cfargument name="value" required="true" type="string" > 

    <cfreturn StructNew() />   
    </cffunction> 

</cfcomponent> 

のWebService URL

http://test.com/webservice/Sample.CFC?method=get&name=test

+1

代わりに、既存のフレームワークを使用する方がよいでしょう。あなたはTaffyを見ましたか? http://docs.taffy.io/ –

答えて

2

第二の方法は、CFMLでWebサービスを行うための標準的な方法です。この機能に加えて、標準ベースのWSDLの返品と定義を得ることを求めています。それはホイールを再構築する場合です。私は、wsの根底にあるCFコードが最適化できると確信していますが、それはそのままで、数百万のフィールドテストを受けています。

0

ColdFusionでRESTful Webサービスを設定することをお勧めします。開始するにはexcellent articleがあります。

Taffyもありますが、私はそれを使用していませんが、簡略化しています。

関連する問題