Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
152 Chapter 7 REST Programming // PUT (Update) def onUpdate() { // Get the ID of car to update, this param is fixed def carId = request.params.carId[] logger.INFO {"Update starting for: ${carId}"} // The data to update the record with could be in any param def carData = request.params.someData[] // Update data for existing car updateCar( carData ) // Assume this method exists // We don't need to return anything on this update. request.status = HttpURLConnection.HTTP_NO_CONTENT } // DELETE def onDelete() { // Get the ID of car to delete, this param is fixed def carId = request.params.carId[] logger.INFO {"Delete attempted for: ${carId}"}. | 152 Chapter 7 REST Programming PUT Update def onUpdate Get the ID of car to update this param is fixed def carld request.params.carId logger.INFO Update starting for carId The data to update the record with could be in any param def carData request.params.someData Update data for existing car updateCar carData Assume this method exists We don t need to return anything on this update. request.status HttpURLConnection.HTTP_NO_CONTENT DELETE def onDelete Get the ID of car to delete this param is fixed def carld request.params.carId logger.INFO Delete attempted for carId Lets not allow deletion of Cars request.status HttpURLConnection.HTTP_BAD_METHOD As you can see this is easy to grasp. Each REST method that your resource supports is directly mapped to one of these methods. For methods that expect a specific identifier such as onRetrieve you can see that WebSphere sMash has the convention of using the entity name appended with Id in our example each specific reference is found in the request parameters as carId. This will become more important when we discuss binding resources later in the chapter. Creating a PHP Resource Handler Creating a PHP handler is similar to the Groovy handler. It s a matter of placing a php file within the app resources virtual directory where the filename is directly mapped to the resource it represents. The contents of the PHP resource files can be defined in two different ways depending on your preference. In Listing 7.2 we have a basic PHP script that represents the same car resource as we defined in Groovy. One issue to observe is that the same GET request is used for list and singleton reads. You need to test the event _name variable to determine the proper intent of the request as shown in the listing. Download from www.wowebook.com Creating a PHP Resource Handler 153 Listing 7.2 app resources car.php Car Resource Handler in PHP php method zget request method switch method GET Singleton and Collection case GET if zget event _name list