Rest Services Consuming with Java Script Se Rest


























- Slides: 26

Rest Services Consuming with Java. Script Se Rest rvi ce s Soft. Uni Team Technical Trainers Software University http: //softuni. bg

Table of Contents 1. RESTful Design 1. GET Requests 2. POST Requests 3. PUT Requests 4. DELETE Requests 2

Have a Question? sli. do #Java. Web 3

RESTful Design 4

RESTful Design 5

HTTP GET § Used to retrieve data arrays Web Client GET /items Response [ { "id": 32, "name": “Read Book", "deadline": 1362268800000, "category. Name": "Work", "enabled": false }, … ] Server 6

HTTP GET § Used to retrieve single data entities Web Client GET /items/1 Response { "id": 32, "name": “Read Book", "deadline": 1362268800000, "category. Name": "Work", "enabled": false } Server 7

Problem 1 Get Available Categories 8

Get Available Categories Category. Controller. java @Get. Mapping("/categories") public Response. Entity<List<Category. View. Model>> get. All. Categories(){ List<Category. View. Model> categories = this. category. Service. get. All. Categories(); return new Response. Entity(categories, Http. Status. OK ); } Returns Categories in JSON 9

Consume Available Categories jquery $. ajax({ type: 'GET', GET Request URL url: '/categories', data. Type: 'json', Response Type content. Type: 'application/json', Sent Type to Server success: function (data) { … Response Data }); 10

HTTP POST § Used to save data Web Client Data to Save { "id": 32, "name": “Read Book", "deadline": 1362268800000, "category. Name": "Work", "enabled": false } Server POST /items Response OK Response 11

Problem 2 Save Item 12

Save Item. Controller. java @Post. Mapping(path = "/items") public Response. Entity<To. Do. Item. View. Model> save. Item (@Request. Body To. Do. Item. Binding. Model to. Do. Item. Binding. Model) { JSON Model To. Do. Item. View. Model to. Do. Item. View. Model = this. to. Do. Item. Service. save. Item(to. Do. Item. Binding. Model); return new Response. Entity(to. Do. Item. View. Model, Http. Status. OK); } Return Saved Item with ID 13

Save Item via AJAX jquery $. ajax({ type: 'POST', url: '/items', Item to save data: JSON. stringify(item), data. Type: 'json', content. Type: "application/json", success: function (to. Do. Item) { … } Response Item with Id } 14

HTTP PUT § Used to update data. It is idempotent - can be called many times without different outcomes Web Client Update Item with Id = 1 Data to Update { "id": 32, PUT /items/1 "name": “Read News", "deadline": 1362268800000, "category. Name": "Work", Response "enabled": false } Server OK Response 15

Problem 3 Update Item 16

Update Item. Controller. java @Put. Mapping(path = "/update/{item. Id}") public Response. Entity<List<To. Do. Item. View. Model>> update. Item(@Request. Body To. Do. Item. Binding. Model to. Do. Item. Binding. Model , @Path. Variable long item. Id) { JSON Model this. to. Do. Item. Service. update. Item(item. Id , to. Do. Item. Binding. Model); return new Response. Entity(Http. Status. OK); } Return OK 17

Update Item via AJAX jquery $. ajax({ type: 'PUT', url: '/items/update/' + item. Id, data: JSON. stringify(current. Item), content. Type: 'application/json' }) Dynamic URL 18

HTTP DELETE § Used to delete data. It is idempotent - can be called many times without different outcomes Web Client Delete Item with Id = 1 Server DELETE /items/delete/1 Response OK Response 19

Problem 4 Delete Item 20

Delete Item. Controller. java @Delete. Mapping(path = "items/delete/{item. Id}") public Response. Entity delete. Item(@Path. Variable long item. Id) { this. to. Do. Item. Service. delete. By. Id(item. Id ); return new Response. Entity(Http. Status. OK); } Return OK 21

Delete Item via AJAX jquery $. ajax({ type: 'DELETE', url: '/items/delete/' + id, content. Type: 'application/json' }) Dynamic URL 22

Summary § RESTful design - one way of providing interoperability between computer systems on the Internet 23

Web Development Basics – Course Overview ? s n stio e u Q ? ? ? https: //softuni. bg/courses/

License § This course (slides, examples, demos, videos, homework, etc. ) is licensed under the "Creative Commons Attribution. Non. Commercial-Share. Alike 4. 0 International" license 25

Free Trainings @ Software University § Software University Foundation – softuni. org § Software University – High-Quality Education, Profession and Job for Software Developers § softuni. bg § Software University @ Facebook § facebook. com/Software. University § Software University @ You. Tube § youtube. com/Software. University § Software University Forums – forum. softuni. bg