Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.artivisi.latihan.web; import com.artivisi.latihan.model.Role; import com.artivisi.latihan.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * * @author jimmy */ @RestController @RequestMapping("/api") public class RoleController { @Autowired private RoleService roleService; @RequestMapping(value = "/role", method = RequestMethod.GET) public Iterable<Role> tampilkanSemuaRole() { return roleService.findAll(); } @RequestMapping(value = "/role/{id}", method = RequestMethod.GET) public Role tampilkanRoleBerdasarkanId(@PathVariable(value = "id") String idRole) { return roleService.findOne(idRole); } @RequestMapping(value = "/role", method = RequestMethod.POST) public void simpanRole(@RequestBody Role role) throws Exception { if (role == null) { throw new Exception("Role tidak boleh null"); } roleService.save(role); } @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE) public void hapusRole(@PathVariable(value = "id") String idRole) throws Exception { if (idRole.trim().length() <= 0 || idRole == null) { throw new Exception("id tidak boleh kosong atau null"); } Role role = roleService.findOne(idRole); if (role == null) { throw new Exception("Role dengan id tidak ditemukan"); } roleService.delete(role); } @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT) public void update(@PathVariable String id, @RequestBody Role x) throws Exception { Role role = roleService.findOne(id); if (role == null) { throw new Exception("Role dengan id tidak ditemukan"); } x.setId(role.getId()); roleService.save(x); } }