restful services with coldfusion
TRANSCRIPT
Powerpoint TemplatesPowerpoint Templates
ColdFusion and APIsKevin [email protected] @kevpocalypse
Powerpoint Templates
Who am I?
Kevin SchmidtManaging Partner
Email: kevin,[email protected] Twitter: @kevpocalypse
Powerpoint Templates
About REST
What is REST?
REST (REpresentational State Transfer) is an architectural style, and an approach to communications that is often used in the development of Web services.
Why REST?
The use of REST is often preferred over the more heavyweight SOAP (Simple Object Access Protocol) style because REST does not leverage as much bandwidth, which makes it a better fit for use over the Internet. The SOAP approach requires writing or using a provided server program (to serve data) and a client program (to request data).
Powerpoint Templates
REST Details
REST Contraints: Decouples consumers from producers Stateless existence Able to leverage a cache Leverages a layered system Leverages a uniform interface
REST Verbs:•POST - Create•GET - Read•PUT - Update•DELETE – Delete
Want to Geek Out?Roy Fielding’s Dissertation:http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.html
Powerpoint Templates
ColdFusion and REST
You can create REST services by defining certain attributes in the tags cfcomponent, cffunction, and cfargument and publish as REST resources.
•Follows HTTP request-response model: Beyond having HTTP as a medium, the service lets you follow all HTTP norms. The components published as REST services can be consumed over HTTP/HTTPS request. The REST services are identified with URI (Uniform Resource Identifier) and can be accessed from a web page as well as by specifying the URI in the browser's address bar.
•Supports all HTTP methods : The REST enabled CFCs support the following HTTP methods: GET, POST, PUT, DELETE, HEAD, and OPTIONS.
•Implicit handling of serialization/deserialization: ColdFusion natively supports JSON and XML serialization/deserialization. So client applications can consume REST services by issuing HTTP/HTTPS request. The response can either be serialized to XML or JSON format.
•Publish web service as both REST service and WSDL service: You can create and publish the same ColdFusion component as a REST service and WSDL service.
Powerpoint Templates
ColdFusion and REST
Creating the REST web service
You can create and publish a ColdFusion component or any functions in a component as REST resource.
To create a CFC as REST web service, specify either of the following in the tag cfcomponent: restPath or rest.
In cffunction, set the attribute access to remote for the functions that you have to expose as REST resource.
Example:<cfcomponent rest="true" restpath="/person">
<cffunction name="getAll” returntype="string” access="remote” httpmethod="GET” produces="application/json”>
<cfset var response = Application['PersonGateway'].getAll() />
<cfreturn serializeJSON(response) />
</cffunction></cfcomponent>
Powerpoint Templates
ColdFusion and REST
Registering an application with the REST service
After you create the CFC you want to REST-enable, specify the folder for registering as web service either using the autoRegister Application setting, the function restInitAplication() or in the ColdFusion Administrator or using the ColdFusion Admin API.
If you are in a shared environment:
<cfset this.restsettings.autoregister = true />
restInitApplication(rootPath[,serviceMapping[,options]])
These options not require administrator privileges.
Powerpoint Templates
ColdFusion and REST
Sample URI:http://localhost:8500/rest/restTest/restService
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.
Powerpoint Templates
ColdFusion and REST
Default Response Description200 OK Sent if the response has a body.
204 No Content Sent if the response doesn’t have a body.
Default Response Description404 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 A resource is unable to consume the MIME type of the 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.
Custom responses can be created using the restSetResponse method for success or <cfthrow type=“RestError”> for errors.
Powerpoint Templates
ColdFusion API Manager
The API Manager includes the following functionalities, such as:
•Access control: The API Manager helps you to restrict certain APIs to trusted subscribers and enforce usage for each users based on roles.•Analytics: You can track the usage of an API across applications, methods, and users.•Portal: There are separate portals for an API administrator, publisher, and subscriber with custom workflows.