Guest User

Untitled

a guest
Feb 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package com.hexaware;
  2.  
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.POST;
  5. import javax.ws.rs.PUT;
  6. import javax.ws.rs.DELETE;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.Produces;
  9. import javax.ws.rs.Consumes;
  10. import javax.ws.rs.PathParam;
  11. import javax.ws.rs.core.MediaType;
  12. import com.hexaware.model.Customer;
  13. import java.util.*;
  14.  
  15. /**
  16. * Root resource (exposed at "myresource" path)
  17. */
  18. @Path("customers")
  19. public class CustomerController {
  20.  
  21. private static List<Customer> customerList = new ArrayList<>();
  22.  
  23. @GET
  24. @Produces(MediaType.APPLICATION_JSON)
  25. public List<Customer> fetchAllCustomers(){
  26. return this.customerList;
  27. }
  28.  
  29. @GET
  30. @Path("/{id}")
  31. @Produces(MediaType.APPLICATION_JSON)
  32. public Customer fetchCustomerById(@PathParam("id") int id){
  33. return this.customerList.get(id);
  34. }
  35.  
  36. @POST
  37. @Consumes(MediaType.APPLICATION_JSON)
  38. @Produces(MediaType.APPLICATION_JSON)
  39. public Customer addCustomer(Customer customer){
  40. this.customerList.add(customer);
  41. return customer;
  42. }
  43.  
  44. @PUT
  45. @Path("/{id}")
  46. @Consumes(MediaType.APPLICATION_JSON)
  47. @Produces(MediaType.APPLICATION_JSON)
  48. public Customer updateCustomer(@PathParam("id") int id, Customer customer){
  49. customerList.add(id, customer);
  50. return customerList.get(id);
  51. }
  52.  
  53. @DELETE
  54. @Path("/{id}")
  55. public void deleteCustomer(@PathParam("id") int id){
  56. customerList.remove(id);
  57. }
  58. }
Add Comment
Please, Sign In to add comment