Created
November 13, 2018 18:25
-
-
Save tofuninjah/90a8648142503211f359e649a3f84261 to your computer and use it in GitHub Desktop.
Sample Spring RESTful controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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