Skip to content

Instantly share code, notes, and snippets.

@tofuninjah
Created November 13, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tofuninjah/90a8648142503211f359e649a3f84261 to your computer and use it in GitHub Desktop.
Save tofuninjah/90a8648142503211f359e649a3f84261 to your computer and use it in GitHub Desktop.
Sample Spring RESTful controller
package com.abc.vendor.controllers;
import com.abc.vendor.entities.Vendor;
import com.abc.vendor.repos.VendorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/vendors")
public class VendorRestController {
@Autowired
VendorRepository vendorRepository;
/** All **/
@GetMapping
public List<Vendor> getAllVendors(){
return vendorRepository.findAll();
}
/** Create **/
@PostMapping
public Vendor create(@RequestBody Vendor request) {
return vendorRepository.save(request);
}
/** Read **/
@RequestMapping("/{id}")
public Vendor findVendor(@PathVariable("id") int id) {
return vendorRepository.findById(id).get();
}
/** Update **/
@PutMapping
public Vendor update(@RequestBody Vendor request) {
return vendorRepository.save(request);
}
/** Delete **/
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") int id) {
Vendor vendor = vendorRepository.findById(id).get();
vendorRepository.delete(vendor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment