Whatever message this page gives is out now! Go check it out!
<cfcomponent rest="true" restpath="restService">
<cffunction name="sayHello" access="remote" returntype="String" httpmethod="GET">
<cfset rest = "Hello World">
<cfreturn rest>
</cffunction>
</cfcomponent><cfset restinit("c:\restfolder","myapp")>
<cfset restinit("\map","myapp")><cfhttp
url="http://localhost:8500/rest/RestTest/restService"
method="get"
port="8500"
result="res">
</cfhttp>URL component | Description |
http://localhost:8500 | Base URL which includes the IP address and port of the ColdFusion server.If you deploy ColdFusion as a JEE application, the URL will contain a context root, for example, http://localhost:8500*/cfusion* . |
rest | Implies that the request sent is a REST request.This default value can be renamed by revising the context path in web.xml available at cfusion/wwroot/WEB-INF and update the same mapping in uriworkermap.properties file found at config\wsconfig\1. |
restTest | Application name or service mapping that you have used while registering the service in ColdFusion Administrator. If you do not specify a service mapping in the ColdFusion Administrator, then the application name is taken from Application.cfc. |
restService | Rest path you defined in the service. That is, the value of the attribute restPath in the tag cfcomponent. |
GET http://adobe.com/stuff Accept: application/xml, application/json |
GET http://adobe.com/stuff Accept: text/*;q=0.9, */;q=0.1, audio/mpeg, application/xml;q=0.5 |
GET http://adobe.com/stuff Accept: text/*;q=0.9, */;q=0.1, audio/mpeg=0.9, application/xml;q=0.5 |
<cfcomponent rest="true" restPath="/hello">
<cffunction name="sayHello" access="remote" returnType="String" httpMethod="GET" restPath="name">
---------
</cffunction>
</cfcomponent><cfcomponent rest="true" restpath="/studentService">
<cffunction name="addStudent" access="remote" returntype="void" httpmethod="POST">
<cfargument name="name" type="string" required="yes" restargsource="Form"/>
<cfargument name="age" type="numeric" required="yes" restargsource="Form"/>
<!--- Adding the student to data base. --->
</cffunction>
<cffunction name="getStudent" access="remote" returntype="student" restpath="{name}-{age}">
<cfargument name="name" type="string" required="yes" restargsource="Path"/>
<cfargument name="age" type="string" required="yes" restargsource="Path"/>
<!--- Create a student object and return the object. This object will handle the request now. --->
<cfset var myobj = createObject("component", "student")>
<cfset myobj.name = name>
<cfset myobj.age = age>
<cfreturn myobj>
</cffunction>
</cfcomponent><cfcomponent>
<cfproperty name="name" type="string"/>
<cfproperty name="age" type="numeric"/>
<!--- Getting the student detail. --->
<cffunction name="getStudent" access="remote" returntype="String" httpmethod="GET" produces="text/xml">
<!--- Retrieve the Student from the DB. --->
<!--- get student from db where name and age matches --->
<cfset st.name = "Thomas">
<cfset st.age = "25">
<cfset st.surname = "Paul">
<cfset str = "<student><name>" & st.name & "</name><age>" & st.age & "</age><surname>" & st.surname & "</surname></student>">
<cfreturn str>
</cffunction>
<!--- Updating the student detail. --->
<cffunction name="updateStudent" access="remote" returntype="void" httpmethod="PUT">
<!--- Retrieve the Student from the DB. --->
<!--- Update the student in DB. --->
<cfset st.name = name>
<cfset st.age = age>
</cffunction>
<!--- Deleting the student. --->
<cffunction name="deleteStudent" access="remote" returntype="String" httpmethod="DELETE">
<!--- Delete the Student from the DB. --->
<!---<cfset st = deleteStudentFromDB(name)>--->
<cfreturn "Student deleted">
</cffunction>
</cfcomponent><!--- adding the student --->
<cfhttp url="http://localhost:8500/rest/RestTest/studentService" method="post" result="res">
<cfhttpparam type="formfield" name="name" value="Thomas" >
<cfhttpparam type="formfield" name="age" value="25" >
</cfhttp>
<cfoutput>Student Added</cfoutput>
</br>
</br>
<!--- fetching the details --->
<cfhttp url="http://localhost:8500/rest/RestTest/studentService/Thomas-25" method="get" result="res">
</cfhttp>
<cfoutput>#xmlformat(res.filecontent)#</cfoutput>
</br>
</br>
<!--- updating the student details --->
<cfhttp url="http://localhost:8500/rest/RestTest/studentService/Thomas-25" method="put" result="res">
</cfhttp>
<cfoutput>Updated the student</cfoutput>
</br>
</br>
<!--- deleting the student --->
<cfhttp url="http://localhost:8500/rest/RestTest/studentService/Thomas-25" method="delete" result="res">
</cfhttp>
<cfoutput>Student Deleted</cfoutput>Default Response | Description |
200 OK | Sent if the response has body . |
204 No Content | Sent if the response does not have body . |
Default Response | Description |
404 Not found | Request URL is not valid. |
406 Not Acceptable | No function in the REST service can produce the MIME type requested by the client. |
415 Unsupported Media Type Error | A resource is unable to consume the MIME type of client request. |
405 Method not allowed | If the client invokes an HTTP method on a valid URI to which the request HTTP method is not bound.This error does not appear if the client requests for HTTP HEAD and OPTIONS methods.If a resource method that can service HEAD requests for that particular URI is not available, but there exists a method that can handle GET, it is invoked and returns the response from that method without the request body. If there is no existing method that can handle OPTIONS, a meaningful automatically generated response is sent along with the Allow header set. |
<cffunction name="create" httpMethod="POST" produces="application/xml" returnType="void">
<cffunction><cfset response=structNew()>
<cfset response.status=201>
<cfset response.content="<customer id="&id&"><name>"&name&"</name></customer>"> <cfset response.headers=structNew()>
<cfset response.headers.location="http://localhost:8500/rest/CustomerService/customers/123">restSetResponse( response ); |
<cffunction name="getCustomer" httpMethod="GET" produces="application/xml" restPath="{id}" return="string">
<cfargument name="id" type="numeric" argtype="PathParam">
<!--- Getting the customer. ---><cffunction><cfthrow type="RestError" errorcode="404">Variable | Description |
this.restsettings.cfclocation | To publish the CFCs only in a particular location, provide comma-separated list of directories where the REST CFCs are located. The directory paths can be absolute or relative.If not set, all the CFCs from the application root are published. |
this.restsettings.skipCFCWithError | When an error occurs, continue publishing, ignoring the CFC that has caused the exception.If true, the CFC with error is ignored and the rest of the CFCs are published. By default it is false.If set to false, in case of an error, the application itself is not published. But other registered application are published.If an error occurs during application startup, the error is printed in console .Each application has separate log files for logging the issues. |
<cfcomponent>
<cffunction name="SayPlainHello" access="remote" produces="text/plain" returntype="string" httpmethod="POST">
<cfreturn "BaseCustomerResource Plain">
</cffunction>
<cffunction name="SayXMLHello" access="remote" produces="text/xml,application/xml" returntype="string" httpmethod="POST">
<cfreturn "BaseCustomerResource XML">
</cffunction>
</cfcomponent><cfcomponent rest="true" restpath="/customerservice" extends="BaseCustomerResource">
<cffunction name="SayPlainHello" access="remote" returntype="string">
<!--- Implement the method. --->
<cfreturn "CustomerResource Plain">
</cffunction>
<cffunction name="SayXMLHello" access="remote" returntype="string">
<!--- Implement the method. --->
<cfreturn "CustomerResource XML">
</cffunction>
</cfcomponent><cfhttp url="http://localhost:8500/rest/RestTest/customerservice" method="post" result="res">
<cfhttpparam type="header" name="accept" value="text/plain" >
</cfhttp>
<cfoutput>#res.filecontent#</cfoutput>
</br>
</br>
<cfhttp url="http://localhost:8500/rest/RestTest/customerservice" method="post" result="res">
<cfhttpparam type="header" name="accept" value="application/xml" >
</cfhttp>
<cfoutput>#res.filecontent#</cfoutput>
</br>
</br><cfcomponent rest="true" restPath="/customerservice" extends="BaseCustomerResource">
<cffunction name="SayPlainHello" access="remote" produces="text/plain" returntype="string" httpmethod="PUT">
<cfargument name="username" type="string" argtype="pathparam">
<!--- Implement the method. --->
</cffunction>
<cffunction name="SayXMLHello" access="remote" returntype="string">
<cfargument name="username" type="string">
<!--- Implement the method. --->
</cffuntion>
</cfcomponent><cfset this.arr1 = arrayNew(1)>
<cfset this.arr1[1] = "1">
<cfset this.arr1[2] = this.arr1>
<cfset this.arr1[3] = "3"><array id="1" size="3">
<item index="1" type="string">1</item>
<item index="2" type="array">
<array id="2" size="1">
<item index="1" type="string">1</item>
</array>
</item>
<item index="3" type="string">3</item>
</array><query id="1">
<columnnames>
<column name="columnnameone"/>
<column name="columnnametwo"/>
</columnnames>
<rows>
<row>
<column type="string">value one</column>
<column type="string">value two</column>
</row>
<row>
<column type="number">444.0</column>
<column type="string">value four</column>
</row>
</rows>
</query><struct id="1">
<entry name="name" type="string">joe</entry>
<entry name="age" type="string">30</entry>
<entry name="address" type="string">101 some drive, pleasant town, 90010</entry>
<entry name="eyecolor" type="string">blue</entry>
<entry name="income" type="string">50000</entry>
</struct><component id="1" name="testrest.student">
<property name="name" type="string">paul</property>
<property name="age" type="number">444.0</property>
<property name="dob" type="date">478377000000</property>
</component><array id="1" size="2">
<item index="0" type="struct">
<struct id="2">
<entry name="name" type="string">joe</entry>
<entry name="age" type="string">30</entry>
<entry name="address" type="string">101 some drive, pleasant town, 90010</entry>
<entry name="eyecolor" type="string">blue</entry>
<entry name="income" type="string">50000</entry>
</struct>
</item>
<item index="1" type="struct">
<struct id="3">
<entry name="name" type="string">paul</entry>
<entry name="age" type="string">25</entry>
<entry name="address" type="string">some other address</entry>
<entry name="eyecolor" type="string">black</entry>
<entry name="income" type="string">40000</entry>
</struct>
</item>
</array><cfcomponent>
<cfproperty name="str" type="string"/>
</cfcomponent><cfcomponent restpath="arrayOfCFC">
<cffunction name="func1" access="remote" output="false" returntype="arrayCFCdefinition[]"
httpmethod="get" produces="text/xml">
<cfset arrCFC = arraynew(1)>
<cfloop from=1 to=2 index="i">
<cfset obj = createObject("component", "arrayCFCdefinition")>
<cfset obj.str = i>
<cfset arrayAppend(arrCFC, obj)>
</cfloop>
<cfreturn arrCFC>
</cffunction>
<cffunction name="func2" access="remote" output="false" returntype="arrayCFCdefinition[]"
httpmethod="get" produces="text/json">
<cfset arrCFC = arraynew(1)>
<cfloop from=1 to=2 index="i">
<cfset obj = createObject("component", "arrayCFCdefinition")>
<cfset obj.str = i>
<cfset arrayAppend(arrCFC, obj)>
</cfloop>
<cfreturn arrCFC>
</cffunction>
</cfcomponent><cfhttp url="http://127.0.0.1:8500/rest/RestTest/arrayOfCFC" method="get" result="res1">
<cfhttpparam type="header" name="accept" value="text/xml">
</cfhttp><cfhttp url="http://127.0.0.1:8500/rest/RestTest/arrayOfCFC" method="get" result="res2">
<cfhttpparam type="header" name="accept" value="text/json">
</cfhttp><array id="1" size="2" type="cfsuite.restservices.restservices.new.ArrayCFCdefinition">
<item index="1" type="COMPONENT">
<component id="2" name="cfsuite.restservices.RESTServices.New.arrayCFCdefinition">
<property name="STR" type="NUMBER">
1.0
</property>
</component>
</item>
<item index="2" type="COMPONENT">
<component id="3" name="cfsuite.restservices.RESTServices.New.arrayCFCdefinition">
<property name="STR" type="NUMBER">
2.0
</property>
</component>
</item>
</array>[{"Str":1},{"Str":2}]<cfcomponent>
<cfproperty name="str" type="string"/>
<cffunction name="check" returntype="any">
<cfreturn this.str>
</cffunction>
</cfcomponent><cfcomponent>
<cffunction name="func3" access="remote" output="false" returntype="string"
httpmethod="put" produces="text/xml" consumes="text/xml">
<cfargument name="arg" type="arrayCFCdefinition[]"/>
<cfif arg[2].check() eq "2">
<cfreturn "true">
<cfelse>
<cfreturn "false">
</cfif>
</cffunction>
</cfcomponent><cfhttp url="http://127.0.0.1:8500/rest/RestTest/arrayOfCFC" method="put" result="res3">
<cfhttpparam type="header" name="content-type" value="text/xml">
<cfhttpparam type="header" name="accept" value="text/xml">
<cfhttpparam type="body" value="<ARRAY ID=""1"" SIZE=""2"" TYPE=""cfsuite.restservices.restservices.new.ArrayCFCdefinition""><ITEM INDEX=""1"" TYPE=""COMPONENT""><COMPONENT ID=""2"" NAME=""cfsuite.restservices.restservices.new.ArrayCFCdefinition""><PROPERTY NAME=""STR"" TYPE=""NUMBER"">1.0</PROPERTY></COMPONENT></ITEM><ITEM INDEX=""2"" TYPE=""COMPONENT""><COMPONENT ID=""3"" NAME=""cfsuite.restservices.restservices.new.ArrayCFCdefinition""><PROPERTY NAME=""STR"" TYPE=""NUMBER"">2.0</PROPERTY></COMPONENT></ITEM></ARRAY>">
</cfhttp><struct id="1">
<entry name="arrayinastruct" type="array">
<array id="2" size="2">
<item index="0" type="struct">
<struct id="3">
<entry name="name" type="string">joe</entry>
<entry name="age" type="string">30</entry>
<entry name="address" type="string">101 some drive, pleasant town, 90010</entry>
<entry name="eyecolor" type="string">blue</entry>
<entry name="income" type="string">50000</entry>
</struct>
</item>
<item index="1" type="struct">
<struct idref="3"/>
</item>
</array>
</entry>
</struct><cfset this.arr1 = arrayNew(1)>
<cfset this.arr1[1] = "1">
<cfset this.arr1[2] = this.arr1>
<cfset this.arr1[3] = "3">[1,[1],3]{'COLUMNS':['columnNameOne','columnNameTwo'],'Data':[['value one','value two'],['444.0','value four']]}{'NAME':'joe','AGE':30,'ADDRESS':'101 Some Drive, Pleasant Town, 90010','EYECOLOR':'blue','INCOME':50000}{'NAME':'Paul','AGE':444.0,'DOB':'July, 27 2011 00:00:00'}[{'NAME':'joe','AGE':30,'ADDRESS':'101 Some Drive, Pleasant Town, 90010','EYECOLOR':'blue','INCOME':50000},{'NAME':'paul','AGE':25,'ADDRESS':'Some other address','EYECOLOR':'black','INCOME':40000}]<cfset this.restsettings.autoregister="true"/><cfset this.restsettings.servicemapping="testmapping"/><cfset this.restsettings.usehost=true/><cfset this.restsettings.host="www.adobe.com"/><cfset this.restsettings.isDefault=true/>restInitApplication(rootPath[,serviceMapping[,options]])<cfset str=structNew()>
<cfset str.host = "www.site1.com:82">
<cfset str.isDefault = "true">
<cfset RestInitApplication("C:\dev\ColdFusion\cf_main\cfusion\wwwroot\withhostAndDefault", "withhostAndDefault", str)>
App registered<cfset str=structNew()>
<cfset str.useHost = "true">
<cfset str.isDefault = "true">
<cfset RestInitApplication("C:\dev\ColdFusion\cf_main\cfusion\wwwroot\withhostAndDefault", "withhostAndDefault", str)>
App registeredYou do not need Administrator privileges to perform the task. The syntax is: restInitApplication(rootPath[,serviceMapping[,options]]) |
Struct
[
name(String],
phoneNo(Struct)
[
code(String),
no(String)
]
]
Struct
[
name(String],
phoneNo(Struct)
[
code(String),
no(String)
]
]<Array size="1"> <Item index="1"> <root> <Name>Paul</Name> <PhoneNo> <root> <Code>080</Code> <No>411150326</No> </root> </PhoneNo> </root> </Item> </Array><cfcomponent>
<cffunction name="serialize" access="remote" returntype="String">
<cfargument name="arg" type="any" hint="The object to be serialized"/>
<cfargument name="type" type="string"
hint="The accept header of the request in array format. The order of types in the array is according to the priority of the MIME types in the header."/>
<cfset var result = "">
<cfset var key = "">
<cfif arguments.type eq "XML">
<cfif isStruct(arguments.arg)>
<cfset result = "<root>">
<cfloop collection="#arguments.arg#" item="key">
<cfset result = result & "<" & key & ">">
<cfset result = result & serializeXML(arguments.arg[key], true)>
<cfset result = result & "</" & key & ">">
</cfloop>
<cfset result = result & "</root>">
<cfreturn result>
<cfelse>
<!--- SerializeXML is a new function added in ColdFusion 11. This function will serialize the object to XML
using ColdFusion's default serialization mechanism." --->
<cfreturn serializeXML(arguments.arg)>
</cfif>
<cfelseif arguments.type eq "JSON">
<cfdump var="#arguments.arg.getClass().getName()#" output="console">
<cfif arguments.arg.getClass().getName() eq "java.lang.String">
<cfreturn "test" & arguments.arg>
<cfelse>
<cfreturn serializeJSON(arguments.arg)>
</cfif>
<cfelse>
<!--- Serialize is a new function added in ColdFusion 11. This function will serialize the object to a
a specified type using ColdFusion's default serialization mechanism. --->
<cfreturn serialize(arguments.arg, arguments.type)>
</cfif>
</cffunction>
<cffunction name="canSerialize" access="remote" returntype="boolean">
<cfargument name="type" type="string"/>
<cfif arguments.type eq "XML">
<cfreturn true>
<cfelseif arguments.type eq "JSON">
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="canDeserialize" access="remote" returntype="boolean">
<cfargument name="type" type="string"/>
<cfif arguments.type eq "XML">
<cfreturn true>
<cfelseif arguments.type eq "JSON">
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="deserialize" access="remote" returntype="any">
<cfargument name="arg" type="String" hint="The string to be deserialized"/>
<cfargument name="type" type="String" hint="The content-type header of the request."/>
<cfset var xmlDoc = "">
<cfset var result = "">
<cfset var numEntries = "">
<cfset var key = "">
<cfset var value = "">
<cfif arguments.type equals "XML" and isXml(arguments.arg)>
<cfset xmlDoc = xmlParse(arguments.arg)>
<cfif xmlDoc.XmlRoot.XmlName equals "root">
<cfset result = StructNew()>
<cfset numEntries = ArrayLen(xmlDoc.root.XMLChildren)>
<cfloop index="i" from="1" to="#numEntries#">
<cfset key = xmlDoc.root.XMLChildren[i].XmlName>
<cfif ! len(Trim(xmlDoc.root.XMLChildren[i].XMLText))>
<cfset value = deserializeXML(ToString(xmlDoc.root.XMLChildren[i].XMLChildren[1]), true)>
<cfelse>
<cfset value = deserializeXML(xmlDoc.root.XMLChildren[i].XMLText, true)>
</cfif>
<cfset result[key] = value>
</cfloop>
<cfreturn result>
<cfelse>
<cfreturn deserializeXML(arguments.arg, true)>
</cfif>
<cfelse>
<cfreturn deserializeXML(arguments.arg, true)>
</cfif>
</cffunction>
</cfcomponent><cfset this.customSerializer="CustomSerializer">