1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 } } |
1 2 3 4 5 6 7 8 | $ curl --request POST http://localhost:9090/trip/airport Create $ curl --request GET http://localhost:9090/trip/airport Retrieve $ curl --request PUT http://localhost:9090/trip/airport Update $ curl --request DELETE http://localhost:9090/trip/airport Delete |
1 2 3 4 5 6 7 8 9 10 | class UrlMappings { static mappings = { "/$controller/$action?/$id?"{ constraints { // apply constraints here } } "/rest/airport/$iata?"(controller:"airport",action:"index") "500"(view:'/error') } } |
1 2 3 4 5 6 7 8 9 10 11 | def index = { switch(request.method){ case "POST": //... case "GET": if(params.iata){render Airport.findByIata(params.iata) as XML} else{render Airport.list() as XML} break case "PUT": //... case "DELETE": //... } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | def index = { switch(request.method){ case "POST": //... case "GET": //... case "PUT": //... case "DELETE": if(params.iata){ def airport = Airport.findByIata(params.iata) if(airport){ airport.delete() render "Successfully Deleted." } else{ response.status = 404 //Not Found render "${params.iata} not found." } } else{ response.status = 400 //Bad Request render """DELETE request must include the IATA code Example: /rest/airport/iata """ } break } } |
1 2 3 4 5 | Deleting a Good Airport</heading> $ curl --verbose --request DELETE http://localhost:9090/trip/rest/airport/lga > DELETE /trip/rest/airport/lga HTTP/1.1 < HTTP/1.1 200 OK Successfully Deleted. |
1 2 3 4 | $ curl --verbose --request DELETE http://localhost:9090/trip/rest/airport/foo > DELETE /trip/rest/airport/foo HTTP/1.1 < HTTP/1.1 404 Not Found foo not found. |
1 2 3 4 5 | $ curl --verbose --request DELETE http://localhost:9090/trip/rest/airport > DELETE /trip/rest/airport HTTP/1.1 < HTTP/1.1 400 Bad Request DELETE request must include the IATA code Example: /rest/airport/iata |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |