Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hexaware;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.PUT;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.MediaType;
- import com.hexaware.model.Customer;
- import java.util.*;
- /**
- * Root resource (exposed at "myresource" path)
- */
- @Path("customers")
- public class CustomerController {
- private static List<Customer> customerList = new ArrayList<>();
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- public List<Customer> fetchAllCustomers(){
- return this.customerList;
- }
- @GET
- @Path("/{id}")
- @Produces(MediaType.APPLICATION_JSON)
- public Customer fetchCustomerById(@PathParam("id") int id){
- return this.customerList.get(id);
- }
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Customer addCustomer(Customer customer){
- this.customerList.add(customer);
- return customer;
- }
- @PUT
- @Path("/{id}")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Customer updateCustomer(@PathParam("id") int id, Customer customer){
- customerList.add(id, customer);
- return customerList.get(id);
- }
- @DELETE
- @Path("/{id}")
- public void deleteCustomer(@PathParam("id") int id){
- customerList.remove(id);
- }
- }
Add Comment
Please, Sign In to add comment