![Page 1: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/1.jpg)
and
get
![Page 2: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/2.jpg)
Groovy; what’s that?
Scripting language based on Java syntax that compiles into bytecode for the same JVM Java runs on
Similar to Ruby and Python
Adds closures, dynamic method support, makes semi-colons optional
Has JSR legitimacy
![Page 3: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/3.jpg)
Grails; where’s Indiana Jones?
Modeled after Rails, follows many of the same conventions, but runs in a Servlet container with just one additional jar file.
But build on existing Java technology: Spring, Hibernate, Sitemesh, prototype, ant, hsqldb (no not-invented-here-syndrome)
Has an excellent plugin ecosystem, plugins for most popular Java libraries: Acegi, Jasper, terracotta, Axis2, YUI/ext/jquery and lots
![Page 4: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/4.jpg)
Back to RESTful
Some code examples from Scott Davis’s developer works article. Nice guy, VP of Boulder Java Users Group.
http://www.ibm.com/developerworks/library/j-grails09168/
![Page 5: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/5.jpg)
get RESTful
add “import grails.converters.*” to top of your controller class
remember your url will map based on your controller name; so ClassController will map to http://localhost:8080/<app name>/class/
![Page 6: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/6.jpg)
Tweak grails routingclass UrlMappings { static mappings = { "/class/$callNumber?"(controller:"class",action:"index") "/$controller/$action?/$id?"{ constraints { // apply constraints here } } "500"(view:'/error') }}
![Page 7: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/7.jpg)
Tell controller to Accept put/post/delete
def index = { switch(request.method){ case "POST": render "Create\n" break case "GET": render "Retrieve\n" break case "PUT": render "Update\n" break case "DELETE": render "Delete\n" break } }
![Page 8: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/8.jpg)
get
def get(params) { if (params.callNumber) { render CuClass.findByCallNumber(params.callNumber) as XML } else { render CuClass.list() as XML } }
![Page 9: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/9.jpg)
putdef put(params) { def cuClass = new CuClass(params.cuClass) if(cuClass.save()){ response.status = 201 // Created render cuClass as XML } else{ response.status = 500 //Internal Server Error render "Could not create new CuClass due to errors:\n ${cuClass.errors}" } }
![Page 10: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/10.jpg)
postdef post(params) { def cuClass = CuClass.findByCallNumber(params.cuClass.callNumber) cuClass.properties = params.cuClass if(cuClass.save()){ response.status = 200 // OK render cuClass as XML } else{ response.status = 500 //Internal Server Error render "Could not update CuClass due to errors:\n ${cuClass.errors}" } }
![Page 11: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/11.jpg)
deletedef delete(params) { if(params.callNumber){ def cuClass = CuClass.findByCallNumber(params.callNumber) if(cuClass){ cuClass.delete() render "Successfully Deleted." } else{ response.status = 404 //Not Found render "${params.callNumber} not found.\n" } } else{ response.status = 400 //Bad Request render "DELETE request must include the Call Number\n Example: /class/CSCI7818 \n" } }
![Page 12: and - University of Colorado Boulderkena/classes/7818/f08/lectures/lecture_8_getting_restful...Grails; whereÕs Indiana Jones? Modeled after Rails, follows many of the same conventions,](https://reader034.vdocuments.mx/reader034/viewer/2022042307/5ed3cb1c7ac79b44002012b6/html5/thumbnails/12.jpg)
Tips
new lines in XML are very bad, put everything on the same line
grails can be a cruel mistress, I don’t like its error reporting, but rails isn’t any better. Django experience?
curl reports content size wrong and the server doesn’t read all of the XML