springspring-bootcrud

Spring Boot full REST CRUD example


Does anyone have a full spring boot REST CRUD example? The spring.io site just has a RequestMapping for GET. I'm able to get POST and DELETE working but not PUT.

I suspect it's how I'm trying to get the params where the disconnect is, but I have not seen an example where someone is performing an update.

I'm currently using the SO iPhone app so I can't paste my current code. Any working examples would be great!


Solution

  • As you can see I have implemented two way to update. The first one will receive a json, and the second one will receive the cusotmerId in the URL and json also.

    @RestController
    @RequestMapping("/customer")
    public class CustomerController {
    
    
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public Customer greetings(@PathVariable("id") Long id) {
            Customer customer = new Customer();
            customer.setName("Eddu");
            customer.setLastname("Melendez");
            return customer;
        }
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public List<Customer> list() {
            return Collections.emptyList();
        }
    
        @RequestMapping(method = RequestMethod.POST)
        public void add(@RequestBody Customer customer) {
    
        }
    
        @RequestMapping(method = RequestMethod.PUT)
        public void update(@RequestBody Customer customer) {
    
        }
    
        @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
        public void updateById(@PathVariable("id") Long id, @RequestBody Customer customer) {
    
        }
    
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public void delete() {
    
        }
    
        class Customer implements Serializable {
    
            private String name;
    
            private String lastname;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public void setLastname(String lastname) {
                this.lastname = lastname;
            }
    
            public String getLastname() {
                return lastname;
            }
        }
    }